How to print out 2 variables using disp() function

623 次查看(过去 30 天)
In my script:
  • Create 4 variables: len, wid, txt, area
  • In 'len' and 'wid' stores two numbers 2 and 3 respectively
  • Perform multiplication of 'len' and 'wid' and store the result in the variables 'area'
  • In 'txt' store the character array 'The area is: '
  • Use disp() function to display 'txt' and 'area' together.
My code looks like this, but the output is weird and I don't know what's the problem, the output suppose to be "The area is: 6"

采纳的回答

Stephen23
Stephen23 2020-8-12
编辑:Stephen23 2020-8-12
Use concatentation to join the character vectors together into one longer character vector:
disp([txt,num2str(area)])
"...the output is weird and I don't know what's the problem..."
Using plus on two character vectors adds their character codes.
  2 个评论
Huy Truong Tan Gia
Huy Truong Tan Gia 2020-8-12
Thank you so much! I'm just a beginner in MATLAB.
But what exactly is the problem with my program? What are those numbers? What is the '[' and ']' for?
Walter Roberson
Walter Roberson 2020-8-12
In MATLAB, the syntax [something] is used to construct vectors or arrays.
The syntax [A,B] is (nearly) the same as horzcat(A, B) which is the same as cat(2, A, B) which concatenates along the second dimension. Another way of saying that is that if you have two arrays A and B that have the same number of rows, then [A,B] gives you a new array that has all of the columns of A followed by all of the columns of B. [txt, num2str(area)] takes the vector of characters that is in the variable txt and puts the vector of characters resulting from num2str(area) immediately after it (no spaces or commas are automatically added.)
The problem with your program:
Characters are represented internally as numbers, together with a flag that says "Treat this as characters". Each different character has an associated arbitrary number, with there being international standards about which numbers should be associated with which character. The character 'T' happens to be associated with the (decimal) number 84, and there are functions that you can pass the decimal number 84 and it will mark it as text and then the display routines will know to put up a glyph representing 'T' when asked to display it.
Computers do not have a separate internal storage mechanism for characters and decimal numbers and floating point numbers: computers just have sequences of bits together with other sequences of bits that say how the first sequence of bit are intended to be interpreted. The sequence of bits 01010100 is used to represent both the decimal number 84 and the character 'T' .
Now, your code has two character vectors and uses the '+' operation between them. When you have character vectors and ask to use the + operation, then MATLAB is defined to remove the flag that says that the bits are to be interpreted as text, and to instead interpret the bits as a number. The value at the memory location does not change: what changes is the interpretation . So your + causes the text to be reinterpreted as the underlying numbers that are used to encode your text, and the addition between numbers is done. And then unless instructed otherwise, the result is left together header information that says that the list of underlying numbers is to be interpreted as numbers instead of as text. Then when you ask for the values to be displayed, MATLAB looks and sees the bits are marked as being numbers, so it converts the numbers into characters representing the decimal digits of the number, for display purposes -- so bits 01010100 get converted to decimal 84, which gets converted to the sequence of characters '8' then '4' (which in turn is really the sequence of decimal values 56 (representing '8') then 52 (representing '4') except marked as representing characters...)
Anyhow, those 138 and so on are the result of doing addition between underlying numbers that represented characters -- 'T' + '6' is 84 ('T') + 54 ('6') = 138 . If you had happened to ask MATLAB to reinterpret those as text, it would have ended up being 'S'
This happened because you asked to add 'The area is' plus '6' .

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by