How to display two things on one line?
468 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Cole Bromfield
el 15 de En. de 2020
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 comentarios
per isakson
el 15 de En. de 2020
Editada: per isakson
el 15 de En. de 2020
Homework I assume.
You have to do it with one disp() statement, because disp() automatically adds a newline after the output
Respuesta aceptada
Más respuestas (2)
per isakson
el 15 de En. de 2020
Editada: per isakson
el 16 de En. de 2020
>> "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"
0 comentarios
Paul
el 3 de Mzo. de 2023
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 comentarios
Walter Roberson
el 3 de Mzo. de 2023
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.
Ver también
Categorías
Más información sobre Large Files and Big Data en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!