MATLAB Answers

Why does my output formatting not print multiple times?

4 views (last 30 days)
This is the best I can get from this MATLAB 2016 Student source code:
fmt_num10='%10.4f%10.4f%10.4f%10.4f%10.4f%10.4f%10.4f%10.4f%10.4f%10.4f\n';
fmt_str80='%s\n'
for ken=1,KENG;
fprintf(fidout,'TITJ\n')
fprintf(fidout,fmt_str80,TITJ(k,:))
fprintf(fidout,'D6,D7,D8,D10,D11,D12,D13,D14,D15,D16\n')
fprintf(fidout,fmt_num10,[D6(k),D8(k),D10(k),D11(k),D12(k),D13(k), ...
D14(k),D15(k),D16(k)])
end
I'm expecting the following KENG times, but it only prints the last request:
TITJ
*---MAIN GEAR DWT---*
D6,D7,D8,D10,D11,D12,D13,D14,D15,D16
-97.7830 0.0000 221.2340 10.4380 0.9146 0.0000 0.0000 0.0000 0.4043

  2 Comments

Kenneth Lamury
Kenneth Lamury on 16 Jul 2016
Sorry about that, added D7(k) to the mix. It still only prints the first listing, not others. Any other suggestions?
Kenneth Lamury
Kenneth Lamury on 16 Jul 2016
Again, sorry about that. Changing ken=1,KENG to k=1:KENG, and adding D7(k) to output string fixed my problems.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Jul 2016
One possibility is that your for loop increments ‘ken’ but the only vector reference I can see references ‘k’. I suspect you define ‘k’ somewhere else (perhaps in another loop) in your code earlier, and its value remains the maximum value it reaches in that loop, so the subscript reference would be to that unchanging value.

  2 Comments

Kenneth Lamury
Kenneth Lamury on 16 Jul 2016
Sorry about that. Changed ken to k. But now it only prints the first reading and not all of them. Any further comments?
Star Strider
Star Strider on 16 Jul 2016
It should go through the entire loop and print everything at each iteration.
I don’t have your data, but when I do a similar loop:
mtx = randi(9, 5, 6);
fmt = ['\t' repmat('%3.0f', 1, size(mtx,2)) '\n'];
for k1 = 1:size(mtx,1)
fprintf(1, 'Row %.0f:\n',k1)
fprintf(1, fmt, mtx(k1,:))
end
it prints out correctly.
I just noticed that you left out ‘D(7)’ here:
fprintf(fidout,fmt_num10,[D6(k),D8(k),D10(k), ...
Not critical, but you may want to include it.
You can also use the repmat function here:
fmt_num10 = [repmat('%10.4f', 1, 10) '\n'];

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Jul 2016
for k=1:KENG
not
for k=1,KENG

  1 Comment

Kenneth Lamury
Kenneth Lamury on 16 Jul 2016
Sorry about that. I need to remember to switch my thinking caps. Still applying FORTRAN & MathCad logic to MATLAB.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by