Multiple lines with fprintf

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 comentarios

Adam Danz
Adam Danz el 3 de Abr. de 2020
How is the output messed up? We can't run your code due to missing variable values.
Sorry about that Adam Danz, here is the entire segment of code if you want to see it. I added the file too. Don't fix my code though, because this is homework and I don't want to cheat, only pay attention to the fprintf part that I mentioned in the question. Also the code is a function so I put it in two different scripts.
FILENAME = 'AU_SB_2020_08.txt';
% ***** INPUT *****
% open data file
[fid, msg] = fopen(FILENAME, 'r');
% does file exist?
if fid <= 0
fprintf( 'File not available\n' )
else
% file exist continue
help legend
[month, day, sc1, sc2, s1, s2, s3, s4] = textread(FILENAME, ...
'%f%f%*s%*s%f%f%f%f%f%f');
date = [month, day];
scores = [sc1, sc2];
stats = [s1, s2, s3, s4];
reportGraph(date, scores, stats)
fclose(fid);
end
Here is the function
function [] = reportGraph(date, scores, stats)
% User defined function to print the report and the plot
month = date(:, 1);
day = date(:, 2);
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);
final = [month, day, sc1, sc2, s1, s2, s3, s4, aveRound];
num = length(final);
rowNum = 0;
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];
fprintf('2020 AU Softball Batting Stats as of 03/01\n')
fprintf('Date Score W/L AB Runs Hits HRuns Ave\n')
for n=1:num
rowNum = rowNum + 1;
row = final(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
fprintf('Ave: %04.1f-%04.1f %4.1f %3.1f %3.1f %3.1f %5.3f\n', ave)
end
dpb
dpb el 3 de Abr. de 2020
>> fprintf('%02.0f/%02.0f %02.0f-%02.0f L %1.0f %2.0f %2.0f %1.0f %5.3f\n', ...
rand(9,1)*20)
16/18 03-18 L 13 2 6 11 19.150
>> fprintf('%02.0f/%02.0f %02.0f-%02.0f W %1.0f %2.0f %2.0f %1.0f %5.3f\n', ...
rand(9,1)*30)
29/05 29-29 W 15 24 4 13 27.472
>>
seems to work ok, presuming your variable row always has exactly nine (9) elements to match up to the number of fields in the format string.
Adam Danz
Adam Danz el 3 de Abr. de 2020
Editada: Adam Danz el 3 de Abr. de 2020
If month and day are integers, you can use %d instead of %f.
Otherwise, I don't see what's messed up. Could you described the expected output compared to the actual output?
dpb
dpb el 3 de Abr. de 2020
num = length(final);
may be a place you've got problem...
length isn't what most think it is--
length(x) --> max(size(x))
so if the array of x is more rows than columns --> size(x,1) while if more columns than rows, then --> size(x,2)
So, you can get an unexpected result depending upon the size of your input array.
It appears you want nRows above, so use
num=size(final,1);
Cole Rasmussen
Cole Rasmussen el 3 de Abr. de 2020
Editada: Cole Rasmussen el 3 de Abr. de 2020
Thanks for all the feedback you guys, I think I've got it now.
Adam Danz
Adam Danz el 3 de Abr. de 2020
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 el 3 de Abr. de 2020
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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre MATLAB Mobile Fundamentals en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 3 de Abr. de 2020

Comentada:

dpb
el 3 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by