putting a subscript into a string, displaying the command window
76 views (last 30 days)
Show older comments
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 Comment
Demetrius
on 22 Nov 2024 at 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)+"]");
Answers (2)
Walter Roberson
on 18 Nov 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 Comments
Guillaume
on 18 Nov 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.
Guillaume
on 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.
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!