Problems with creating a dynamic struct with num2str

5 ビュー (過去 30 日間)
Rodrigo Sánchez Suárez
Rodrigo Sánchez Suárez 2021 年 1 月 3 日
回答済み: Ive J 2021 年 1 月 3 日
for i = 1:9
M.[num2str(i)] = zeros(3,3);
end
This gives me error, I have never really understood the syntaxis of num2str or how to use it with combination of text. This has probably a really simple answer but I havent been able to find it. Thanks for your time.

採用された回答

Ive J
Ive J 2021 年 1 月 3 日
First of all you cannot choose a digit as filed name due to the same reason you cannot choose it as a variable name.
for i = 1:9
M.(['x', num2str(i)]) = zeros(3,3);
end
Or if you prefer strings:
for i = 1:9
M.("x" + i) = zeros(3,3);
end

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by