Rename field arrays of a structure that hold identical name
2 views (last 30 days)
Show older comments
Unai Vicente
on 13 Dec 2019
Commented: Unai Vicente
on 13 Dec 2019
Hi there,
I have some previously processed data that is identically named by variable but not by .mat name. I believe it would have been better to assign from the beginning some sort of dinamically generated naming to the output but it didn't happen and now is too late.
After I load the data and organize it into a structure with the following code:
alldat=[];
for i=1:18
alldat=[alldat;load(['CCD_Dyad',num2str(i),'.mat'])];
end
The code at first does what I want, it takes the CCD_Dyad archives with the different numeration (from 1 to 18) and it places them inside a structure as you can see in the following pic.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/254335/image.png)
Now the problem is with the name, as all the fields, filled with 54x54 matrices, have identical names. Now, as they are identically named I cannot use them and perform operations (such as a simple mean between all the matrices) as it reports an error of "Too many input arguments" which makes sense if all the fields are named the same way.
I've been diving into the forums but I do not find any answers fit to my problem. Then I'd like any way to dinamically rename (by number) the fields, or a way to print the name of the file (which contains the number) into the variable, so I can have them differentiated and capable to perform operations with.
Any help would be much appreciated.
Thanks in advance
2 Comments
Turlough Hughes
on 13 Dec 2019
Have you tried the following?
mean2(alldat(1).CCD_dyad)
mean2(alldat(2).CCD_dyad)
etc
Accepted Answer
Turlough Hughes
on 13 Dec 2019
I'm guessing your variable is called CCD_Dyad when you load a given .mat file, so given that you have 54 by 54 matrices I would recommend that you put them in a 3d matrix as follows:
alldat=NaN(54,54,18);
for i=1:18
load(['CCD_Dyad',num2str(i),'.mat']);
alldat(:,:,i)=CCD_Dyad;
end
AvOfMatrices=mean(alldat,3);
If you had matrices of different sizes you could consider using cell arrays.
You could also convert from your structure to get the 3d matrix:
data=NaN(54,54,18);
for c=1:size(alldat,2)
data(:,:,c)=alldat(c).CCD_dyad;
end
AvOfMatrices=mean(data,3);
4 Comments
Turlough Hughes
on 13 Dec 2019
Happy to help. I just added in the last comment for your information because it was the title of your question but yes the 3d matrix is definitly much more preferable.
More Answers (0)
See Also
Categories
Find more on Structures 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!