Multiplication of two fields in a structure array

8 ビュー (過去 30 日間)
SS
SS 2020 年 1 月 15 日
コメント済み: SS 2020 年 1 月 16 日
Hi. I want to perform element wise multiplication of two fields in a struture array. The size of the fields is same throughout for a given Struct(i). When, I use the code below it gives the following error:
"Argument to dynamic structure reference must evaluate to a valid field name"
for i=1:length(Struct)
Struct(i).dx_dc1=(Struct(i).L1).*(cos(Struct(i).phi1));
Struct(i).dy_dc=(Struct(i).L1).*(sin(Struct.(i).phi1));
Struct(i).dx_dc2=(Struct(i).L2).*(cos(Struct(i).phi2));
Struct(i).dz_dc=(Struct(i).L2).*(sin(Struct(i).phi2));
end
  2 件のコメント
adeq123
adeq123 2020 年 1 月 15 日
Hi,
Can you attach the Struct as well ?
Guillaume
Guillaume 2020 年 1 月 15 日
Are you sure that it's the exact code with no modification that you're running?
This code doesn't have any dynamic structure reference so it would be very surprising for you to get the error stated. Now, if you had something like:
Struct(i).(something) %brackets around something make it a dynamic struct ref
then yes, you could get this type of error.

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

採用された回答

Steven Lord
Steven Lord 2020 年 1 月 15 日
You made a typo on this line.
Struct(i).dy_dc=(Struct(i).L1).*(sin(Struct.(i).phi1));
There shouldn't be a period between Struct and (i) in the sin call.
Struct(i).dy_dc=(Struct(i).L1).*(sin(Struct(i).phi1));
  2 件のコメント
Guillaume
Guillaume 2020 年 1 月 15 日
Well spotted! This is indeed the dynamic struct ref.
SS
SS 2020 年 1 月 16 日
Thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by