str2num error with text field

조회 수: 1 (최근 30일)
Lucia Wagner
Lucia Wagner 2020년 7월 8일
답변: Anirudh Singh 2020년 7월 13일
Hello MATLAB community!
I am receiving this error... Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
...for this line of code (marked with comment). Any suggestions would be quite helpful!
function TranslateXEditFieldValueChanged(app, event)
value = app.TranslateXEditField.Value;
switch app.ConeTypeSelected
x = str2num(get(app.TranslateXEditField, 'value')) % this line of code is receiving the error
case 1
case 2
case 3
end
end
  댓글 수: 5
jonas
jonas 2020년 7월 9일
A bit unrelated, but you may want to use a numeric edit field, unless you expected mixed data of course. You can also access the value field as app.TranslateXEditField.Value, so no need for get(...,'value')
dpb
dpb 2020년 7월 9일
Indeed...it does look like the parser state machine was going on trying to make an expression...what, specifically it did internally to generate the specific error I've no idea, of course. It may be one of those "when all else fails, hoist the white flag" and give up.
I just tried now (which I didn't earlier) -- in the editor, when you enter an executable statement inside the switch block but outside a case block you get a generic
parse error at {first word on line}: usage might be invalid MATLAB syntax.
which would be more informative probably if got that at runtime as well.
Shows shouldn't try to run code w/ red marks on RH column in editor! :)
Does look like this is one that ought to be able to be identified explicitly as
"code in the SWITCH not contained in CASE."

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

답변 (1개)

Anirudh Singh
Anirudh Singh 2020년 7월 13일
As said dpb, The statement is in a switch construct, but not inside a case block.
The MATLAB switch does not work like other language switch.
switch switch_expression
case case_expression
statements
case case_expression
statements
...
otherwise
statements
end
You can check the Tips on the given documentation: https://www.mathworks.com/help/matlab/ref/switch.html

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by