putting a subscript into a string, displaying the command window

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 Kommentar

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)+"]");

Melden Sie sich an, um zu kommentieren.

Antworten (2)

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 Kommentare

Marco
Marco am 18 Nov. 2015
Bearbeitet: Marco am 18 Nov. 2015
so cprintf(2,G<sub>5)? Sorry but I'm going to need more than this: Say I have a string, 'G', I need to display it with a subscript number, not in the GUI, in the command window
No, you do not need cprintf itself, but it is a reference example of how to use the technique.
input: fprintf(2,G<sub>5) output: Undefined function or variable 'sub'.
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.

Melden Sie sich an, um zu kommentieren.

Guillaume
Guillaume am 18 Nov. 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 Kommentar

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.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 18 Nov. 2015

Kommentiert:

am 22 Nov. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by