フィルターのクリア

Why is this fprintf not wprking correctly?

1 回表示 (過去 30 日間)
Hunter Steele
Hunter Steele 2019 年 10 月 28 日
回答済み: Walter Roberson 2019 年 10 月 28 日
studentNames = ["Amy", "Flynn", "Barbara", "George", "Jonathan", "Isaac", "Cindy", "Hank", "Elaine", "Debbie"];
Test1 = [79, 71, 98, 68, 65, 97, 72, 68, 68, 82];
Test2 = [61, 94, 98, 66, 91, 90, 76, 86, 67, 88];
Final = [68, 84, 85, 90, 91, 85, 88, 80, 51, 72];
fileID = fopen('Students.txt','w');
%% A.
for i = 1: length(studentNames)
Avg1 = Test1 * .3;
Avg2 = Test2 * .3;
Avg3 = Final * .4;
TotalAvg = Avg1 + Avg2 + Avg3;
end
fprintf(fileID, '%s has an average of %g \r\n', studentNames,TotalAvg);
  2 件のコメント
Daniel M
Daniel M 2019 年 10 月 28 日
編集済み: Daniel M 2019 年 10 月 28 日
But it is working correctly. Just because you want it to work a certain way, and don't know how to achieve that, doesn't mean there is a bug in the code. fprintf prints in column order.
Hunter Steele
Hunter Steele 2019 年 10 月 28 日
I want it to print a name as %s and the average at %g and it is not doing that.

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

回答 (2 件)

Daniel M
Daniel M 2019 年 10 月 28 日
array = [studentNames;string(TotalAvg)];
fprintf('%s has an avg of %g\r\n',array);

Walter Roberson
Walter Roberson 2019 年 10 月 28 日
You are already using string objects so you might as well use compose() to format the text, possibly fwrite() the results

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by