MATLAB Answers

calling a matrix with different names in a for loop

22 ビュー (過去 30 日間)
Yaser Khojah
Yaser Khojah 2019 年 9 月 5 日
コメント済み: Yaser Khojah 2019 年 9 月 5 日
I have three matrixes with different names as NPV_1, NPV2 and NPV_3. I have tried the below but It did not work. Can you please help
for i = 1:3
fm =forfor sprintf('NPV_%d',i);
NPV_Mat = fm;
end

  2 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2019 年 9 月 5 日
"I have three matrixes with different names as NPV_1, NPV2 and NPV_3"
Accessing separate variable names like that is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Yaser Khojah
Yaser Khojah 2019 年 9 月 5 日
I agree and going to fix it. Thanks a lot for sharing this

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

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2019 年 9 月 5 日
編集済み: Stephen Cobeldick 2019 年 9 月 5 日
Simply put your three matrices into one cell array, then your task is trivial:
C = {NPV_1, NPV2, NPV_3};
for k = 1:numel(C)
M = C{k};
... do whatever with M
end

  1 件のコメント

Yaser Khojah
Yaser Khojah 2019 年 9 月 5 日
Thanks for your help

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

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by