Read struc.m
Jump to navigation
Jump to search
%READ_STRUC2.M
%SRW 11/98,09/99
%Returns an empty structure in parfile not found.
%Reads data into a structure from a file with,
% 1) Field name lines begin with a #,##,###, etc.
% 3) Data lines must include ' ' for strings, and [],{} for arrays and
% can go across lines without contiuation marks (...).
% 3) #@ means put into all elements of that field
% 4) % are comments
% 5) If there are no (')'s or ([)'s in an entry, i.e. a block of text,
% then a padded string array of lines is returned for that field.
function [mystruc,struc_list]=read_struc3(filename)
if (isempty(find(filename=='/')) &...
isempty(find(filename=='\'))),
file=fullfile(pwd,filename);
else
file=filename;
end
struc_name='mystruc';
fid=fopen(file,'r+');
%keyboard
str1=[];
struc_list=[];
while fid~=-1
oline=fgetl(fid);
% allow for comments following %'s
eline=findstr(oline,'%');
if ~isempty(eline),
oline=oline(1:eline-1);
end
if isempty(oline),
%do nothing, go to next line
elseif oline(1)=='#' | oline==-1,
if oline(1)=='#',
struc_field=oline(2:size(parse(oline,1),2));
struc_number=parse(oline,2);
if ~isempty(struc_number),
if struc_number(1)=='@';
struc_number=0; %copy to all strucures in the array
end
end
end
if ~isempty(str1),
str1;
%substitute for any macros, define in top level of structure
while ~isempty(find(str1=='$')),
mac_ind=find(str1=='$');
mac_name=str1((mac_ind(1)+1):(mac_ind(2)-1));
mac_val=eval([struc_name '.' mac_name]);
mac_name=['$' mac_name '$'];
if ~ischar(mac_val),
mac_val=num2str(mac_val);
end
str1=strrep(str1,mac_name,mac_val);
end % while
while ~isempty(findstr('$',str)),
mac_ind=findstr('$',str);
mac_name=str((mac_ind(1)+1):(mac_ind(2)-1));
mac_val=eval([struc_name '.' mac_name]);
mac_name=['$' mac_name '$'];
if ~ischar(mac_val),
mac_val=num2str(mac_val);
end
str=strrep(str,mac_name,mac_val);
end % while
struc_list=strvcat(struc_list, ...
parse(str1(find(str1=='.')+1:find(str1=='=')),1));
if ~isempty(findstr(str,'''')) | ~isempty(findstr(str,'[')) |...
~isempty(findstr(str,'{')),
eval([str1 str ';'])
else
eval([str1 'char(cstr);'])
end
if oline == -1,
fclose(fid);
return
end
end
str1=[struc_name '.' struc_field ' = '];
str=[];
cstr={};
%still need to add @ option to copy to all structures
% $$$ if struc_number>0,
% $$$ str=[struc_name '(' struc_number ')' '.' struc_field ' = '];
% $$$ else
% $$$ str=[struc_name '.' struc_field ' = '];
% $$$ end
else
str=[str oline];
if ~isempty(oline),
cstr=[cstr; {oline}];
end
end
end
eval([struc_name '= []' ';'])
return