Hi,
I am trying to clean some field data using some function. The data are matrix [n x 16]. I would like to generate a structure into a for loop with substructures per each iteration (in this case, per each column).
Here it's the code I wrote so far:
allBIN = [];
nBin = 16; % Number bins
for n = 1:nBin
allVel = [uAll(:,n) vAll(:,n) w1All(:,n) w2All(:,n)]; %get velocity data from each column from uAll, vAll, w1All and w2All
corr = [corrB1(:,n) corrB2(:,n) corrB3(:,n) corrB4(:,n)]; %get correlation data from each column from corrB1, corrB2, corrB3 and corrB4
% Input necessary for CMA function
start=1;
stop=length(uAll(:,1));
thresh=70;
[x,y,z,x_nan,y_nan,z_nan,cleanCorr,cleanVel]=CMA(corr,allVel,start,stop, timeAll',thresh);
% Save cleanCorr and cleanVel from each iteration into structure
allBIN.('n').corr = cleanCorr;
allBIN.('n').vel = cleanVel;
end
I would like to have an output like:
for n = 1:
allBIN.1.corr
allBIN.1.vel
for n = 2:
allBIN.2.corr
allBIN.2.vel
and so on...
Any suggestion?
Thank you!

1 件のコメント

Stephen23
Stephen23 2022 年 5 月 31 日
"I would like to have an output like ... Any suggestion?"
Using a structure array would be simpler and more efficient:
allBIN(1).corr
allBIN(1).vel
..
allBIN(2).corr
allBIN(2).vel
..

サインインしてコメントする。

 採用された回答

Rik
Rik 2022 年 5 月 31 日

0 投票

You should probably use a struct array:
allBIN(n).corr= ___
%etc
But if you insist: field names must be valid variable names, so you need to prepend something:
field=sprintf('it_%d',n);
allBIN.(field).corr= ___
%etc

1 件のコメント

carola forlini
carola forlini 2022 年 5 月 31 日
Hi Rik,
Thank you for your help.
My mistake was to not specify a valid variable name.

サインインしてコメントする。

その他の回答 (1 件)

Davide Masiello
Davide Masiello 2022 年 5 月 31 日

0 投票

Does
allBIN(n).corr
work?

4 件のコメント

carola forlini
carola forlini 2022 年 5 月 31 日
Hi Davide,
I already tried that, and Matlab doesn't allow me to do it.
This is the error message: 'Argument to dynamic structure reference must evaluate to a valid field name'.
Thank you,
Carola
Stephen23
Stephen23 2022 年 5 月 31 日
編集済み: Stephen23 2022 年 5 月 31 日
"This is the error message: 'Argument to dynamic structure reference must evaluate to a valid field name'."
The indexing that Davide Masiello showed will not throw that error. It works without error:
n = 2;
A(n).corr = pi
A = 1×2 struct array with fields:
corr
A(n).corr
ans = 3.1416
However if you try to set a fieldname using an invalid field (e.g. using a numeric) you will get that error:
B.(2).corr = pi
Argument to dynamic structure reference must evaluate to a valid field name.
Davide Masiello
Davide Masiello 2022 年 5 月 31 日
I concur with Stephen.
carola forlini
carola forlini 2022 年 5 月 31 日
Thank you everyone for all the explanations.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 5 月 31 日

コメント済み:

2022 年 5 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by