Using loop to label multiple outputs

9 ビュー (過去 30 日間)
Michael Consolazio
Michael Consolazio 2020 年 2 月 18 日
編集済み: Stephen23 2020 年 2 月 18 日
I have an array X that is one column of values with any number of rows (the number will be determined prior in the program). I want to create a for loop that will make a label for each value and then display the actual value.
Something like this:
X = [11; 22; 33; 44];
for i = 1:length(X)
X(display the value of 'i') = X(i)
end
% With the output on the command window being:
X1 = 11;
X2 = 22;
X3 = 33;
X4 = 44;
How would I go about this? Apologies if my question is poorly phrased.
  1 件のコメント
Ruger28
Ruger28 2020 年 2 月 18 日
check out fprintf in the MATLAB help.

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

採用された回答

Stephen23
Stephen23 2020 年 2 月 18 日
編集済み: Stephen23 2020 年 2 月 18 日
No need for a loop:
>> X = [11;22;33;44];
>> fprintf('X%u = %u\n',[1:numel(X);X(:).']);
X1 = 11
X2 = 22
X3 = 33
X4 = 44
If you really want to use a loop:
for k = 1:numel(X)
fprintf('X%u = %u\n',k,X(k));
end
If you are planning on actually creating numbered variables, read this first:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by