# The numbers do not appear in standard format despite the writing (format shortG)

6 visualizzazioni (ultimi 30 giorni)
almohanned alsufyani il 19 Mar 2023
Risposto: Star Strider il 19 Mar 2023
format shortG
Mf=6.480554950317725e+03;
Cp_sea=3.976;
t_out=100;
t_in=25;
Qu=Mf*Cp_sea*(t_out-t_in); %useful heat gain (kw)
fprintf('useful heat gain = %dKw\n\n',Qu)
_________
In the previous code, the value of Mf was calculated by an equation and the output was as it appeared despite writing (format shortG) in the first line of the code, and then when adding the (fprintf) command, the output also appears in the long Formula e+3, so how do I overcome this problem
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (3)

Dyuman Joshi il 19 Mar 2023
Using format is only applicable to numeric outputs.
Change the formatting operator in fprintf to obtain the proper value -
format shortG
Mf=6.480554950317725e+03;
Cp_sea=3.976;
t_out=100;
t_in=25;
Qu=Mf*Cp_sea*(t_out-t_in) %useful heat gain (kw)
Qu =
1.9325e+06
fprintf('useful heat gain = %g Kw\n\n',Qu)
useful heat gain = 1.9325e+06 Kw
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

VBBV il 19 Mar 2023
Use %f format specifier
format shortG
Mf=6.480554950317725e+03
Mf =
6480.6
Cp_sea=3.976;
t_out=100;
t_in=25;
Qu=Mf*Cp_sea*(t_out-t_in); %useful heat gain (kw)
fprintf('useful heat gain = %.2f kW\n\n',Qu) % use %f
useful heat gain = 1932501.49 kW
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
VBBV il 19 Mar 2023
Try forcing the computed variable using vpa
format shortG
Mf=6.480554950317725e+03
Mf =
6480.6
Cp_sea=3.976;
t_out=100;
t_in=25;
% syms Cp_sea t_out t_in
Qu = vpa(Mf*Cp_sea*(t_out-t_in),8) % somehow
Qu =
1932501.5
% Qu=vpa(subs(Mf*Cp_sea*(t_out-t_in),{Cp_sea,t_out,t_in},{3.976,100,25}),7) %useful heat gain (kw)
fprintf('useful heat gain = %.2f kW\n\n',Qu) % use %f
useful heat gain = 1932501.49 kW

Accedi per commentare.

Star Strider il 19 Mar 2023
One option is to use string arrays —
format shortG
Mf=6.480554950317725e+03;
Cp_sea=3.976;
t_out=100;
t_in=25;
Qu=Mf*Cp_sea*(t_out-t_in); %useful heat gain (kw)
fprintf("useful heat gain = " + Qu + " Kw\n\n")
useful heat gain = 1932501.4862 Kw
.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su General Applications in Help Center e File Exchange

R2022b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by