Issue with fprintf in a loop
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 19 Okt. 2023
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.
Akzeptierte Antwort
Voss
am 19 Okt. 2023
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 Kommentare
Walter Roberson
am 19 Okt. 2023
%.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
am 20 Okt. 2023
Bearbeitet: Stephen23
am 20 Okt. 2023
"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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

