Multiple lines with fprintf

조회 수: 14 (최근 30일)
Cole Rasmussen
Cole Rasmussen 2020년 4월 3일
댓글: dpb 2020년 4월 3일
Hello, so I have a simple question, how do I make multiple lines with fprintf and not mess up the output? I tried making this code into multpile lines using fprintf:
for n=1:num
rowNum = rowNum + 1;
row = finalExp(rowNum, :);
scoreCheck1 = row(:, 3);
scoreCheck2 = row(:, 4);
if scoreCheck1 < scoreCheck2
fprintf('%02.0f/%02.0f %02.0f-%02.0f L %1.0f %2.0f %2.0f %1.0f %5.3f\n', ...
row)
else
fprintf('%02.0f/%02.0f %02.0f-%02.0f W %1.0f %2.0f %2.0f %1.0f %5.3f\n', ...
row)
end
end
but whenever I make the fprintf part a line down (between the %2.0f and the %1.0f) it messes up the output.
  댓글 수: 8
Adam Danz
Adam Danz 2020년 4월 3일
You may want to consider whether it's really necessary to print the help-info on the legend function every time you run the script.
dpb
dpb 2020년 4월 3일
NB: You can use the vectorized operations of MATLAB to good effect...
s1 = stats(:, 1);
s2 = stats(:, 2);
s3 = stats(:, 3);
s4 = stats(:, 4);
sc1 = scores(:, 1);
sc2 = scores(:, 2);
batAve = (s3 ./ s1);
aveRound = round(batAve, 3);
...
sc1Ave = mean(sc1);
sc2Ave = mean(sc2);
s1Ave = mean(s1);
s2Ave = mean(s2);
s3Ave = mean(s3);
s4Ave = mean(s4);
aveAve = mean(aveRound);
ave = [sc1Ave, sc2Ave, s1Ave, s2Ave, s3Ave, s4Ave, aveAve];
...
can be simply
batAve = round(stats(:,3)./stats(:,1), 3);
ave=[mean(scores) mean(stats) mean(batAve)];
As general coding principal, avoid creating additional variables that are named sequentially--use the array indices instead to reference specific columns/rows and the vectorized abilities in ML to do operations over arrays/matrices. It's the power and reason for there being MATrixLABoratory.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Mobile Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by