Read multiple .mat files and process them
Show older comments
Hi,
I wanna read some .mat files with name "sub1_hi.mat" to "sub40_hi.mat" that are 4D matrices and process them.
I did the first step with this code:
for j=1:40
load (['s',num2str(j),'_hi_dDTF.mat'])
end
But, when I wanna try this inner loop I got error for the size and imagesc functions.
for j=1:40
data = load (['s',num2str(j),'_hi_dDTF.mat'])
[chan1, chan2, freq, winNumber] = size(data);
for i=1:winNumber
fig = imagesc(data(:,:,3,i))
filename = ['dDTF_s',num2str(j),'_hi_beta',num2str(i),'.jpg']
saveas(fig, filename)
end
end
Accepted Answer
More Answers (1)
Walter Roberson
on 20 Feb 2023
0 votes
The output of load is always a struct that contains one field for each variable that was loaded. Your second code has the right general approach but you need to account for the loaded variable name as a field of data . That is easy enough if the variable name is always the same, but gets a bit messier if the variable names in the files change.
2 Comments
Reza
on 21 Feb 2023
Walter Roberson
on 21 Feb 2023
Use dynamic field names. Compute the name of the variable and use
data.(variablename)
If the variable name is not known then use fieldnames(data)
Categories
Find more on Loops and Conditional Statements 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!