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 Center および File ExchangeString についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by