MATLAB Answers

Why does my output formatting not print multiple times?

6 views (last 30 days)
This is the best I can get from this MATLAB 2016 Student source code:
for ken=1,KENG;
fprintf(fidout,fmt_num10,[D6(k),D8(k),D10(k),D11(k),D12(k),D13(k), ...
I'm expecting the following KENG times, but it only prints the last request:
-97.7830 0.0000 221.2340 10.4380 0.9146 0.0000 0.0000 0.0000 0.4043
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.
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,:))
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
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!