Help required in populating a structure correctly
Show older comments
Hello,
I am looking to read six (but this number will grow) .mat files into the workspace and am using the following modified code which I found on the Matlab FAQ. The .mat files all contain one variable (an array), and it has an identical name in each of the files. I know that I need to read in the files from their directory and store them in either a structure or a cell array. So far I have been trying unsuccessfully to load them into a structure via this code;
myFolder = 'C:\Users\Garry & Emily\Documents\MATLAB\palms\';
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
matArray = load(fullFileName);
end
A 6x1 structure 'matFiles' is created, but because I am using the dir() fuction to get the filenames, all that gets put into the structure is name date bytes isdir datenum The variable contained in the .mat files is nowhere to be found in this structure.
However, another 1x1 structure "matArray" is also created. This structure only has one entry, the variable/array of the last .mat file read in by load(). I think what's happening here is that because all the .mat file variables share the same name, they are being written and then overwritten by the next .mat file variable.
So, my question is, (a) - do I need to be creating the first structure at all, and (b) - how do I get the second structure to save all the variables and not just the final one.
(I hope this is clear. It's almost 3am where I am and my brain is sleepy, so please feel to ask for any clarifications reqd.)
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion 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!