need to print a matrix with a string

I have a structure composed of 3 students that have a letter for a last name, 3 quiz grades in a matrix, and a few more things. Im supposed to write a for loop to print just the last name and the quiz grades but that means printing a string and a matrix. This is what I tried.
for i=1:3
fprintf('%s %d\n',student(i).lastname,student(i).quiz)
my question is basically how do I print a string with a matrix?

2 件のコメント

madhan ravi
madhan ravi 2018 年 10 月 26 日
I have a structure
show what it looks like
Stephen23
Stephen23 2018 年 10 月 29 日
編集済み: Stephen23 2018 年 10 月 29 日
This is MATLAB, so loops are totally superfluous:
>> S(1).name = 'anna';
>> S(1).quiz = 3;
>> S(2).name = 'bob';
>> S(2).quiz = 4;
>> S(3).name = 'cathy';
>> S(3).quiz = 2;
>> C = [{S.name};{S.quiz}];
>> fprintf('%10s %d\n',C{:})
anna 3
bob 4
cathy 2

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

 採用された回答

madhan ravi
madhan ravi 2018 年 10 月 26 日
編集済み: madhan ravi 2018 年 10 月 26 日

0 投票

or like this maybe?:
student(1).lastname='alex ';
student(1).quiz='3';
student(2).lastname='bob ';
student(2).quiz='4';
student(3).lastname='Alonso ';
student(3).quiz='2';
for i=1:3
string([student(i).lastname,student(i).quiz])
end
COMMAND WINDOW DISPLAYS:
>>
ans =
"alex 3"
ans =
"bob 4"
ans =
"Alonso 2"
>>

4 件のコメント

Carly McKee
Carly McKee 2018 年 10 月 29 日
Thanks! Worked like a charm
madhan ravi
madhan ravi 2018 年 10 月 29 日
Anytime :)
Stephen23
Stephen23 2018 年 10 月 29 日
@madhan ravi: why are you storing numeric data as characters?
madhan ravi
madhan ravi 2018 年 10 月 29 日
was convenient while calling thats why? any better enhancement is welcomed @stephen

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

その他の回答 (1 件)

James Tursa
James Tursa 2018 年 10 月 26 日

0 投票

Assuming there are three grades in the quiz field, maybe this will work for you
fprintf('%s %d %d %d\n',student(i).lastname,student(i).quiz)
If not, then give us a short example of your structure and what you would like for output.

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2018 年 10 月 26 日

コメント済み:

2018 年 10 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by