str2num error with text field
1 view (last 30 days)
Show older comments
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 Comments
jonas
on 9 Jul 2020
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
on 9 Jul 2020
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."
Answers (1)
Anirudh Singh
on 13 Jul 2020
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
0 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!