Issue with fprintf in a loop
11 ビュー (過去 30 日間)
古いコメントを表示
Here is my code :
close all; clear all; clc
g = 1.4; % Specific heat ratio for air
beta = 0:pi/1800:pi/2; % Range for shock wave angle in rad
m = 0;
% theta
for M1 = 1:0.5:10 % Upstream Mach Number from 1 to 10
m = m+1;
p(m)=fprintf('%.2n' , M1);
% theta-beta-M relation
A = ((M1^2)*((sin(beta)).^2))-1;
B = ((g+(cos(2*beta)))*M1^2)+2;
theta = atan(2*cot(beta).*A./B);
% max. theta for a M1
maxtheta(m) = max(theta); % max theta for the Mach No.
maxbeta(m) = beta(theta==maxtheta(m)); % find the beta for max. theta
plot(theta*180/pi,beta*180/pi,'-b')
if M1<=5
x(m)=(theta(beta==60*pi/180)*180/pi);
if x(m)>0
txt= sprintf('M1=%d',p(m));
text(x(m),60,txt)
end
end
hold on
end
plot(maxtheta*180/pi,maxbeta*180/pi,'-r','Linewidth',1.5)
xlabel('\theta')
ylabel('\beta')
axis([0 50 0 90])
When i don't use the fprintf function, the M1 displayed on the graph have many zeros. SO i am trying to get rid of those useless zeros with fprintf. However when I use Fprintf in my loop it only returns 4 and not 1.0 ; 1.5 ; 2.0 and so on.
Could someone help me on that ?
Thank you
2 件のコメント
Stephen23
2023 年 10 月 19 日
Note that the FPRINTF documentation explains that it "returns the number of bytes that fprintf writes". Obtaining the number of bytes is very unlikely to be useful to you. Instead you should format the SPRINTF text in a way that suits your needs.
採用された回答
Voss
2023 年 10 月 19 日
fprintf returns the number of bytes written. It's always zero in this case because '%.2n' is not a valid format.
Instead, use sprintf to construct txt, and use an appropriate format.
close all; clear all; clc
g = 1.4; % Specific heat ratio for air
beta = 0:pi/1800:pi/2; % Range for shock wave angle in rad
m = 0;
% theta
for M1 = 1:0.5:10 % Upstream Mach Number from 1 to 10
m = m+1;
% theta-beta-M relation
A = ((M1^2)*((sin(beta)).^2))-1;
B = ((g+(cos(2*beta)))*M1^2)+2;
theta = atan(2*cot(beta).*A./B);
% max. theta for a M1
maxtheta(m) = max(theta); % max theta for the Mach No.
maxbeta(m) = beta(theta==maxtheta(m)); % find the beta for max. theta
plot(theta*180/pi,beta*180/pi,'-b')
if M1<=5
x(m)=(theta(beta==60*pi/180)*180/pi);
if x(m)>0
txt= sprintf('M1=%.1f',M1);
text(x(m),60,txt)
end
end
hold on
end
plot(maxtheta*180/pi,maxbeta*180/pi,'-r','Linewidth',1.5)
xlabel('\theta')
ylabel('\beta')
axis([0 50 0 90])
6 件のコメント
Walter Roberson
2023 年 10 月 19 日
%.99g is a quick shorthand that people sometimes use to mean "all of the decimal places in the number". But for that purpose it fails for some of the smaller numbers, which is why I tend to use %.999g myself.
The actual maximum number of decimal places needed is
E = eps(0);
Ed = sprintf('%.9999f', E)
Ed = regexprep(Ed, '0+$', '');
L = length(Ed) - 2
fmt = sprintf('%%.%gf', L)
Ed2 = sprintf(fmt, E)
strcmp(Ed, Ed2)
1074 if you use a %f format. If you use a %g format the maximum needed is 754.
Stephen23
2023 年 10 月 20 日
編集済み: Stephen23
2023 年 10 月 20 日
"But whats the difference between %.1f and %.99g ? (Showed on the post below)"
For your use-case %.9g would likely be better. Compare:
V = [1,1.1,1.9876];
fprintf('%.1f\n',V) % fixed-point, one fractional digit
fprintf('%.9g\n',V) % nine significant figures, no trailing zeros
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

