str2num error with text field

1 次查看(过去 30 天)
Lucia Wagner
Lucia Wagner 2020-7-8
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 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by