putting a subscript into a string, displaying the command window

71 visualizaciones (últimos 30 días)
Marco
Marco el 18 de Nov. de 2015
Comentada: Demetrius el 22 de Nov. de 2024 a las 2:06
I need to know how to properly put a character into subscript within a string and to display with the subscript. input: ? output: Look at the picture
Might as well include superscript as well. I care more about being able to display the subscript. Do not include anything including a graph. This is not going to be displayed in a graph. This is going to be displayed in the command window.
  1 comentario
Demetrius
Demetrius el 22 de Nov. de 2024 a las 2:06
Guessing this is a new feature since 2015. Entering this answer for the record. Use the char(#) function where the # is an alt code. You can find the alt codes for certain subscripts here: https://www.webnots.com/alt-code-shortcuts-for-superscript-and-subscript/
An example would be as follows. I'm going to ask the user for the coefficients of an equation. I want the coefficients represented as a11, a12, a13 and b1 for the 1st equation, with all these numbers as subscripts. This line gives me that in my command window:
disp("Enter the coefficients and b value for the 1st equation as a 1 x 4 array")
disp("Equation 1: a"+char(8321)+char(8321)+"x + a"+char(8321)+char(8322)+"y + a"+char(8321)+char(8323)+"= b"+char(8321))
E1=input("Enter as: [a"+char(8321)+char(8321)+" a"+char(8321)+char(8322)+" a"+char(8321)+char(8323)+" b"+char(8321)+"]");

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 18 de Nov. de 2015
See the file exchange contribution cprintf to see how you can use html on things you write to fid 2. Something like
fprintf(2,G<sub>5>
  4 comentarios
Marco
Marco el 18 de Nov. de 2015
input: fprintf(2,G<sub>5) output: Undefined function or variable 'sub'.
Guillaume
Guillaume el 18 de Nov. de 2015
You obviously have to pass a string to fprintf, and if the <sub> tag worked, you would also have to close it. So the syntax would have been
fprintf('G<sub>5</sub>');
However, see my answer. You can't do what you want.

Iniciar sesión para comentar.


Guillaume
Guillaume el 18 de Nov. de 2015
It is not possible to display subscripts or superscripts in the command window. The java text control that matlab uses does not support formatting with only a few exceptions. The only formatting supported is underline, bold, and changing the text colour. All of these are done using undocumented features of matlab (so, may break in a future version of matlab).
underline is achieved using a html hyperlink tag, bold is achieved using the <strong> html tag. You can't have both at once. See cprintf documentation or Yair's (the author of cprintf) blog on the subject.
If matlab supported subscript through html tag the syntax would have been:
fprintf('G<sub>5</sub>');
You obviously have to give a string to fprintf, and you have to close the html tag.
  1 comentario
Marco
Marco el 18 de Nov. de 2015
Thanks. I've been working on a chemical equation solver and this was supposed to be the last step, I've finished everything else. If no better answer exists yours will be the answer. Mark in 6 hours.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by