Rename field arrays of a structure that hold identical name
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Turlough Hughes
2019-12-13
Have you tried the following?
mean2(alldat(1).CCD_dyad)
mean2(alldat(2).CCD_dyad)
etc
采纳的回答
Turlough Hughes
2019-12-13
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 个评论
Turlough Hughes
2019-12-13
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!