Using a for loop and Using fprintf with a mixture of numbers and text in a table

1 回表示 (過去 30 日間)
Bob
Bob 2014 年 12 月 15 日
コメント済み: Image Analyst 2014 年 12 月 15 日
I have the following array:
A=[1; 2; 3; 4];
I then make an array B:
B=[]
I then use a for loop to assign each number in array A a color.
for j=1:length(A)
if A(:,i)=1
B=('blue');
elseif A(:,i)=2
B=('yellow);
elseif A(:,i)=3
B=('green');
else A(:,i)=4;
B=('black');
end
end
The B array has all of the colors.
I want to use fprintf to to have A and B as the headings of the table, where column A has [1; 2; 3; 4] and column B has all of the colors next to their corresponding number from the for loop.
How can I do this?
  2 件のコメント
per isakson
per isakson 2014 年 12 月 15 日
@Image, Sorry I interfered and did the markup of the code

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

採用された回答

Image Analyst
Image Analyst 2014 年 12 月 15 日
I really don't know how this is different than what I already answered in this question. The only difference is that here B is not an array despite you calling it one.
  1 件のコメント
Image Analyst
Image Analyst 2014 年 12 月 15 日
Perhaps you mean this?????
A=[1; 2; 3; 4];
for j= 1 : length(A)
if A(j)==1
B='blue';
elseif A(j)==2
B='yellow';
elseif A(j)==3
B='green';
elseif A(j)==4
B='black';
end
fprintf('%d %s\n', A(j), B);
end

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by