Mierzen
Mierzen

Reputation: 616

Matlab: fprintf ouputs twice and incomplete for some reason

Here is an extract from my code where I use fprintf to write results to the command window:

...
hold off;
X_H2S = (F_H2S_0-F_H2S)/F_H2S_0;
X_COS = (F_COS_0-F_COS)/F_COS_0;
X_CS2 = (F_CS2_0-F_CS2)/F_CS2_0;
plot(W,X_H2S,W,X_COS,W,X_CS2)

fprintf('\nFinal conversions (percent):\n\t\tH2S:  %f\n\t\tCOS:  %f\n\t\tCS2:  %f\n\n',100*X_H2S(size(X_H2S)),100*X_COS(size(X_COS)),100*X_CS2(size(X_CS2)))

fprintf('Pressure drop: %f to %f (%f percent)',P_0,P(size(P)),100*(P_0-P)/P_0)


format longG
%format compact
length = size(fs, 2);
...

For some reason, it outputs as follows:

Final conversions (percent):
        H2S:  24.838056
        COS:  0.000000
        CS2:  99.997968


Final conversions (percent):
        H2S:  0.000000
        COS:  98.985525
        CS2:  0.000000

Pressure drop: 129099.000000 to 128217.024848 (129099.000000 percent)Pressure drop: 0.000000 to 0.000000 (0.000000 percent)Pressure drop: 0.000000 to 0.000000 (0.000000 percent)Pressure drop: 0.000000 to 0.000002 (0.000003 percent)Pressure drop: 0.000005 to 0.000020 (0.000034 percent)Pressure drop: 0.000049 to 0.000196 (0.000343 percent)Pressure drop: 0.000490 to 0.001555 (0.002621 percent)Pressure drop: 0.003687 to 0.007620 (0.011556 percent)Pressure drop: 0.015495 to 0.019436 (0.030585 percent)Pressure drop: 0.041755 to 0.052945 (0.064154 percent)Pressure drop: 0.081088 to 0.098064 (0.115084 percent)Pressure drop: 0.132146 to 0.149250 (0.166397 percent)Pressure drop: 0.183585 to 0.200973 (0.218403 percent)Pressure drop: 0.235875 to 0.253389 (0.270943 percent)Pressure drop: 0.288536 to 0.310239 (0.331999 percent)Pressure drop: 0.353815 to 0.375693 (0.397640 percent)Pressure drop: 0.419669 to 0.445698 (0.471872 percent)Pressure drop: 0.498193 to 0.524642 (0.533730 percent)Pressure drop: 0.542828 to 0.551935 (0.561052 percent)Pressure drop: 0.570178 to 0.579314 (0.596511 percent)Pressure drop: 0.613741 to 0.631004 (0.648300 percent)Pressure drop: 0.665627 to 0.683177 (

Any idea why and how I can fix it?

Upvotes: 0

Views: 1326

Answers (2)

Sushant Kafle
Sushant Kafle

Reputation: 446

It depends on the value that you supplied to the fprintf function. See this output:

>> fprintf('\nFinal conversions (percent):\n\t\tH2S:  %f\n\t\tCOS:  %f\n\t\tCS2:  %f\n\n',[1,1],[1,1],[1,1])

Final conversions (percent):
        H2S:  1.000000
        COS:  1.000000
        CS2:  1.000000


Final conversions (percent):
        H2S:  1.000000
        COS:  1.000000
        CS2:  1.000000

Upvotes: 1

pragmatist1
pragmatist1

Reputation: 919

Your arguments are vectors. If I do this:

>> fprintf('Number: %f\n',rand(2,1))
Number: 0.957507
Number: 0.964889

I observe the same behavior as you. The reason you think you're only printing out one, is because you're calling size, but size returns a 1x2 array containing the number of rows and columns in an array respectively.

Upvotes: 1

Related Questions