plus/minus inside fprinf statement

106 visualizaciones (últimos 30 días)
Marcus Darnell
Marcus Darnell el 18 de Sept. de 2019
Comentada: Walter Roberson el 27 de Sept. de 2025 a las 18:30
Exaclty the title.
I know char(177) returns the plus/minus symbol but can't work out how to get it to display when matlab outputs to the command window.
fprintf('The measured volume of %fmm^{3} \x177 %fmm^{3} is equal to\nthe specified volume of %fmm^{3} \x177 %fmm^{3},\nand therefore the object passes quality inspection and can be released.\n',Vtot, DeltVrounded,VSpec,DeltaVSpec);
  1 comentario
Voss
Voss el 27 de Sept. de 2025 a las 12:31
Since the data you are printing is of character class, use the c conversion in the formatting operator for that data, e.g., '%c':
a = 10;
b = 1.5;
plus_minus_char = char(177);
fprintf('The measured whatever of %f %c %f is equal to the whatever ...',a,plus_minus_char,b)
The measured whatever of 10.000000 ± 1.500000 is equal to the whatever ...

Iniciar sesión para comentar.

Respuesta aceptada

Marcus Darnell
Marcus Darnell el 18 de Sept. de 2019
used \x00B1 and worked

Más respuestas (3)

DNDNDN
DNDNDN el 27 de Sept. de 2025 a las 11:30
Editada: DNDNDN el 27 de Sept. de 2025 a las 11:31
fprintf("%c", 177)
Where 177 is the code for the plus/minus character. E.g.:
fprintf("100%c5", 177)
Outputs:
100±5

dpb
dpb el 18 de Sept. de 2019
pmchar=char(177);
fprintf(['The measured volume of %fmm^{3} ' pmchar '%fmm^{3} is equal to\nthe specified volume of %fmm^{3} ' pmchar '%fmm^{3},\nand therefore the object passes quality inspection and can be released.\n'], ...
Vtot, DeltVrounded,VSpec,DeltaVSpec);
I'd probably write the format string as a variable to be more readable, but that's just a style nit.

Josef Henthorn
Josef Henthorn el 28 de Oct. de 2020
The Latex code for this symbol is \pm
  1 comentario
Walter Roberson
Walter Roberson el 27 de Sept. de 2025 a las 18:30
However, latex formatting is not used for fprintf()

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by