putting a subscript into a string, displaying the command window

조회 수: 23 (최근 30일)
Marco
Marco 2015년 11월 18일
댓글: Demetrius 2024년 11월 22일
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
Demetrius
Demetrius 2024년 11월 22일
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)+"]");

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2015년 11월 18일
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
Marco
Marco 2015년 11월 18일
input: fprintf(2,G<sub>5) output: Undefined function or variable 'sub'.
Guillaume
Guillaume 2015년 11월 18일
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
Guillaume 2015년 11월 18일
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
Marco
Marco 2015년 11월 18일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Adding custom doc에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by