How to display two things on one line?
이전 댓글 표시
My assignmend is telling me to use the display command to display the phrase "The first random variable is" and the x value (calculated earlier in the script) on the same line. The result should be:
The first random variable is 4
Not:
The first random variable is
4
Heres the code (don't worry about the y value)
x=ceil(5*rand(1));
y=floor(99*rand(1));
disp("The first number is ")
disp(x)
댓글 수: 2
per isakson
2020년 1월 15일
편집: per isakson
2020년 1월 15일
Homework I assume.
You have to do it with one disp() statement, because disp() automatically adds a newline after the output
Image Analyst
2020년 1월 15일
Or use fprintf() like I show in my Answer below.
채택된 답변
추가 답변 (2개)
per isakson
2020년 1월 15일
편집: per isakson
2020년 1월 16일
>> "abc"+"def"
ans =
"abcdef"
>>
and your example
>> x = 17;
>> disp( "The first number is " + num2str(x) )
The first number is 17
it's even possible to add the numerical x to the string.
>> disp("The first number is " + x )
The first number is 17
If one input is a string array, then the other input can be a numeric, logical, character, string, or cell array.
>> "true is displayed as "+true
ans =
"true is displayed as true"
Paul
2023년 3월 3일
0 개 추천
Would this be considered a character array or string? I need to output text without using either...
disp("The original number was "+integer+" and the flipped number is "+flipped)
댓글 수: 3
Image Analyst
2023년 3월 3일
Text is either a character array, if single quotes were used to build it, or a string, if double quotes were used to build it.
Why do you need to output text without using either a character array or string? That seems really really weird. Regardless, you can't, since text has to be either one or the other. It can't be "neither".
Walter Roberson
2023년 3월 3일
If the point is that the output must not have the quotation marks, then disp() should work for that purpose.
But I suspect that the restriction is hinting that you should be using fprintf.
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
