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.

댓글 수: 4

Mathieu NOE
Mathieu NOE 2021년 1월 4일
both code works fine on my R2020b
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 1월 4일
It is working perfectly in my system.
Alan Duval
Alan Duval 2021년 1월 4일
Thanks for confirming that I havent just missed something, Mathieu and Kalyan.
Alan Duval
Alan Duval 2021년 1월 4일
I should say, I'm running 2019a to ensure compatibility with my Mac.

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

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2021년 1월 4일
편집: Fangjun Jiang 2021년 1월 4일

0 개 추천

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

Alan Duval
Alan Duval 2021년 1월 4일
Thanks for this. I tried running the one that is failing after a reboot, I also tried 'clear all', thank you, but it still failed.
On your second point, I appreciate that the approach may not be the most graceful, but I'm just trying to get it to work at all to start with. As the age is not going to be used as a number, per se, I'm not hugely concerned just now.
Fangjun Jiang
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.
Alan Duval
Alan Duval 2021년 1월 4일
You're quite right, of course. Thanks to your str2double suggestion I've figured out what the problem was. Thank you.

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

카테고리

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

태그

질문:

2021년 1월 4일

댓글:

2021년 1월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by