Issue with fprintf in a loop
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 19 de Oct. de 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.
Adrien
el 19 de Oct. de 2023
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Entering Commands en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

