Rename field arrays of a structure that hold identical name

3 visualizzazioni (ultimi 30 giorni)
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.
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 Commenti
Turlough Hughes
Turlough Hughes il 13 Dic 2019
Have you tried the following?
mean2(alldat(1).CCD_dyad)
mean2(alldat(2).CCD_dyad)
etc
Unai Vicente
Unai Vicente il 13 Dic 2019
Hi there Turlough,
The problem is I want to average all the matrices by their elements, a1 from array (1) plus a1 out of array (2) until a1 from array (n)/n, so I end up having a 54x54 matrix with the averages of their individual elements. Thanks!

Accedi per commentare.

Risposta accettata

Turlough Hughes
Turlough Hughes il 13 Dic 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 Commenti
Turlough Hughes
Turlough Hughes il 13 Dic 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.
Unai Vicente
Unai Vicente il 13 Dic 2019
Yeah, and thanks for that, too, I'm sure me or anyone else will find it superhelpful having it here :)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by