Incorrect use of '=' operator
7 次查看(过去 30 天)
显示 更早的评论
MATLAB newbie, here.
I'm doing a coding assignment.
This works perfectly fine:
sexq = '(F)emale, (M)ale, (O)ther, (P)refer not to say';
sexa = inputdlg(sexq, 'What is your sex?');
However, when I try to use this:
ageq = 'Participants must be between 18 and 80';
agea = inputdlg(ageq, 'What is your age?');
I consistently get this:
Incorrect use of '=' operator. To assign a
value to a variable, use '='. To compare
values for equality, use '=='.
I've already looked at:
https://uk.mathworks.com/matlabcentral/answers/576694-incorrect-use-of?s_tid=ta_ans_results
I've tried writing it afresh in a new window to see if I'd picked up any text endcoding weirdness having copy/pasted some of the text from a PDF, but it's made no difference. Restarting MATLAB hasn't helped either. Everytime, the first one works, the second one does not.
Thanks in advance.
回答(1 个)
Fangjun Jiang
2021-1-4
编辑:Fangjun Jiang
2021-1-4
Both codes run properly if run alone. You can "clear all" and then try it.
A potential problem is with inputdlg(). "agea" is a cell array. I assume you meant it to be a numerical data.
>> ageq = 'Participants must be between 18 and 80';
agea = inputdlg(ageq, 'What is your age?')
agea =
1×1 cell array
{'18'}
You need to convert it to numerical, for example
>> agea_num=str2double(agea{1})
agea_num =
18
3 个评论
Fangjun Jiang
2021-1-4
The codes in your original question have no problem by their own. When MATLAB reports an error, it also points to the line of code. You should paste the complete error message.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!