0

I have a mat file containing a bunch of structure variables named sequentially: step001,step002,...step070. Each of these variables has a field named 'type'. I want to create a loop to select those variables whose 'type' field is equal to 'rest' and ignore the others.

Any help is highly appreciated. Ehsan

4

1 回答 1

0
%get all the variables in the file:
x = who('-file',name_of_file_string);

for each variable
for i = 1:length(x)

    %construct a new structure S, with the structure as a substructure, using 
    %dynamic naming with sprintf:

    S = load(name_of_file_string,x(i))

    %compare string in 'type' field to 'rest'
    if strcmp(S.(printf('%s',x(i))).type,'rest')
        %if successful, do what you want with:
        S.(printf('%s',x(i))).type
    end

end
于 2012-06-07T23:15:06.803 回答