Convert double type string to data type

%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일

0 개 추천

output1 = compose('Hey, X was %g and Y was %g', X, Y);

댓글 수: 3

I came up with this based on your help and the matlab documentation page
formatSpec = 'The value of X is %d, and the value of Y is %d.';
A1 = X
A2 = Y
XandY = sprintf(formatSpec,A1,A2)
but I received feedback saying that "Variable XandY must be of data type string. It is currently of type char. Check where the variable is assigned a value.At least one character in the text string assigned to XandY is incorrect"
formatSpec = "The value of X is %d, and the value of Y is %d."; %notice " not '
I W
I W 2020년 11월 10일
thank you so much

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

추가 답변 (2개)

Mathieu NOE
Mathieu NOE 2020년 11월 9일

0 개 추천

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일

0 개 추천

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

I W
I W 2020년 11월 9일
thank you. How would I assign this to a variable name XandY. Also, can the sprintf function be used to do this, if so how?
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.

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

I W
2020년 11월 8일

편집:

I W
2021년 12월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by