Help required in populating a structure correctly

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.)

 采纳的回答

Answer to question (a) is Yes.
The reason you only get the variable from the last .mat file is because you are over-write the variable matArray in the loop.
You need to do this before the for-loop, assume the variable name in the .mat file is called Par.
matArray=struct('Par',cell(length(matFiles),1));
Then inside the for-loop, change the last line to be:
matArray(k)=load(fullFileName)
Then you will have matArray(1).Par for the first .mat file, matArray(2).Par for the second .mat file, etc.

9 个评论

Great, thanks for the quick reply. I'll try out what you have suggested and let you know how I get on.
I'm getting an error at the end of the for loop now:
??? The following error occurred converting from struct to double:
Error using ==> double
Conversion to double from struct is not possible.
Error in ==> comparescan at 33
matArray(k) = load(fullFileName);
I never said earlier, my variables within the .mat files are all 'double'. Is this causing this error?
The error has changed to this now:
??? Subscripted assignment between dissimilar structures.
Error in ==> comparescan at 33
matArray(k) = load(fullFileName);
I have no idea what that one means, but i'm going to try google it and see what I come up with.
When you run matArray=load(fullFileName), it is going to create matArray as a structure matArray.Par for the variable Par in the .mat file. I would think matArray could be made a struct array to contain the data for multiple .mat files. But it seems to have problem.
The easiest thing to do is to use cell array. I'll play with my original thought. Use the following instead:
matArray=cell(length(matFiles),1);
Inside the loop, use
matArray{k}=load(fullFileName);
No need to Google. I know what is the problem. It's my early advice without testing. See my comments above. That should work.
Thanks Fangjun, that works!
I'd been reading a lot about people using cell arrays instead of structures so i'm not surprised that I had too as well.
Thanks again for your help, I can go to sleep happy now!
Struct array could work, only if you know the variable name ahead.
Assume your variable name in the .mat file is Par, do this initialization.
matArray=struct('Par',cell(length(matFiles),1))
Then, inside the for-loop, use
matArray(k)=load(fullFileName);
I still think struct array is better than cell array. Both solution could work. I've also updated my answer now.
Yep, that works too! One question, and you gave me two answers!!
Thanks again, you were a great help!

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by