フィルターのクリア

Using Switch/Case to assign values

7 ビュー (過去 30 日間)
Poseidon
Poseidon 2023 年 4 月 20 日
コメント済み: Cris LaPierre 2023 年 4 月 20 日
Hi
I am trying to use switch/case like below but when I run it, says eq is unrecognised variable or function. This is for an app.
switch app.SelectEvaporatorTypeDropDown.Value
case 'Forced Circlation (pumped)'
eq(1)=1;
case 'Falling Film'
eq(1)=2;
case 'Agitated Film (scraped wall)'
eq(1)=3;
case 'Short Tube'
eq(1)=4;
case 'Long Tube'
eq(1)=5
end
  3 件のコメント
DGM
DGM 2023 年 4 月 20 日
I don't see any way that the given code would ever produce that specific error. While eq() is a builtin function, and it's probably a bad idea to be naming your variables after functions, it's not apparent to me how that would be causing this specific error either.
Of course, you didn't include the entire error message backtrace, so it's not clear that this chunk of code is even part of the problem.
Fangjun Jiang
Fangjun Jiang 2023 年 4 月 20 日
Although not recommended, your code does not have error here.
  1. "eq" is a function. use some other variable name, e.g. Myeq
  2. Before this code, pre-allocate the variable and value, Myeq=1;
app.SelectEvaporatorTypeDropDown.Value='Forced Circlation (pumped)';
switch app.SelectEvaporatorTypeDropDown.Value
case 'Forced Circlation (pumped)'
eq(1)=1
case 'Falling Film'
eq(1)=2;
case 'Agitated Film (scraped wall)'
eq(1)=3;
case 'Short Tube'
eq(1)=4;
case 'Long Tube'
eq(1)=5
end
eq = 1

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2023 年 4 月 20 日
編集済み: Cris LaPierre 2023 年 4 月 20 日
I suspect the error is not occuring in the lines of code you have shared, but later on in another function when you try to use eq. Keep in mind variable scope. All your code in an app is inside functions. Once that function completes, its workspace is cleared.
If you want to share data between functions in your app, you must add the variable as a property of the app. See here: https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
You then access it by prepending with the property name with your app object name, like so: app.eq
  4 件のコメント
Poseidon
Poseidon 2023 年 4 月 20 日
編集済み: Poseidon 2023 年 4 月 20 日
Thank you this helped.
There were actually other mistakes I did that was basically causing the problems (messed up the names of the cases and similar mistakes where app and code weren't matching which was causing cases to evaluate false)
Cris LaPierre
Cris LaPierre 2023 年 4 月 20 日
Just an additional note that you can also preallocate your property this way:
properties (Access = private)
Myeq = zeros(1,4); % Description
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by