Convert double type string to data type

7 次查看(过去 30 天)
%These commands generate the random integers described in the problem statement. %Do not overwrite the values of X and Y in your own code. X = randi([1 10]); Y = randi([1 10]); %Enter the commands for your script mfile here. XandY = ([X, Y]) XplusY = ([X + Y])
  1 个评论
dpb
dpb 2020-11-8
The question is totally unclear as to what you are trying to do/ask...give example of what you would want a result to be and for what purpose.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-11-9
output1 = compose('Hey, X was %g and Y was %g', X, Y);
  3 个评论
Walter Roberson
Walter Roberson 2020-11-9
formatSpec = "The value of X is %d, and the value of Y is %d."; %notice " not '

请先登录,再进行评论。

更多回答(2 个)

Mathieu NOE
Mathieu NOE 2020-11-9
hi
simply use the string() command like :
>> X = pi
X =
3.1416
>> str = string(X)
str =
"3.1416"
  1 个评论
I W
I W 2020-11-9
thank you that makes a lot of sense. The question asks the following:
Make a string assigned to the variable XandY and it should present the values by filling in the blank " The value of X is _, and the value of Y is _."
Make a string assigned to the variable XplusY and it should present the sum of the two numbers by filling in the blank " The sum of X and Y is _."
I understand how to get the X and Y values but I do not know how to display it how the question is asking

请先登录,再进行评论。


Mathieu NOE
Mathieu NOE 2020-11-9
so this is simple string concatenation and then disp (for display in command window:
X = pi
Y = 2.234
disp(['The value of X is ' num2str(X) ' and the value of Y is ' num2str(Y) ' bla bla'])
gives
The value of X is 3.1416 and the value of Y is 2.234 bla bla
  3 个评论
Mathieu NOE
Mathieu NOE 2020-11-9
XandY = X +Y;
disp(['The value of X and Y is ' num2str(XandY)])
% you can also use sprintf
% see help sprintf
I W
I W 2020-11-9
I have read through the help page for sprintf but still do not understand it.

请先登录,再进行评论。

类别

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