Assgin a name to sequentially imported files
Show older comments
Experts,
I have imported 8 files sequentially using this piece of code
for i=1:8
fileName=['data' num2str(i)];
dataStruct.(fileName)=load([fileName '.csv']);
end
Now I want to assign a variable to the first imported file, do some operations on it and then save the results. Then, assign the second imported file to the same variable and do the same operations. Then the third one and so on. How can I do that? I mention the WRONG way that I used to be clear below:
for k=1:8
M5=dataStruct.data(k);
[I,J]=size(M5);
BLAH...BLA... BLAH
end
Which results in this error {{{Reference to non-existent field 'data'.}}} Can you please help me? I do appreciate your attention
1 Comment
The cause of the error is quite straightforward: you define each fieldname like this:
['data' num2str(i)]
giving filenames data1, data2, etc. And then you try to access the field data, which does not exist, thus the error. Note that indexing into that field data(k) is totally unrelated to the fieldname.
Accepted Answer
More Answers (2)
Image Analyst
on 25 Feb 2018
0 votes
See the FAQ for much, MUCH better code samples: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
per isakson
on 25 Feb 2018
Edited: per isakson
on 25 Feb 2018
Given dataStruct. An alternative approach
results = structfun( @do_some_operations_on, dataStruct, 'uni',false );
where
function out = do_some_operations_on( data )
BLAH ... BLA... BLAH
end
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!