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

Cris LaPierre
Cris LaPierre 2023년 4월 20일
We are missing some details, as on it's own, this code runs just fine in App Designer.
How/where are you trying to run this code? What is the full error message? Please share all the red text.
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.
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일

0 개 추천

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

properties (Access = private)
Myeq(1,:) % Equipment
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Button
function ButtonValueChanged(app, event)
value = app.Button.Value;
Kvalues=readtable('Kvalues.txt');
switch app.SelectEvaporatorTypeDropDown.Value
case 'Forced Circlation (pumped)'
app.Myeq(1)=1;
case 'Falling Film'
app.Myeq(1)=2;
case 'Agitated Film (scraped wall)'
app.Myeq(1)=3;
case 'Short Tube'
app.Myeq(1)=4;
case 'Long Tube'
app.Myeq(1)=5;
end
switch app.SelectCondenserTypeDropDown
case 'Forced Circlation (pumped)'
app.Myeq(2)=1;
case 'Falling Film'
app.Myeq(2)=2;
case 'Agitated Film (scraped wall)'
app.Myeq(2)=3;
case 'Short Tube'
app.Myeq(2)=4;
case 'Long Tube'
app.Myeq(2)=5;
end
switch app.SelectPumpTypeDropDown.Value
case 'Reciprocating'
app.Myeq(3)=6;
case 'Positive Displacement'
app.Myeq(3)=7;
case 'Centrifugal'
app.Myeq(3)=8;
end
switch app.SelectTurbineTypeDropDown.Value
case 'Axial Gas Turbine'
app.Myeq(4)=9;
case 'Radial Gas/Liquid Expanders'
app.Myeq(4)=10;
end
Evap=app.Myeq(1);
Cond=app.Myeq(2);
Pump=app.Myeq(3);
Turb=app.Myeq(4);
app.EvapEditField.Value=Evap;
app.CondEditField.Value=Cond;
app.PumpEditField.Value=Pump;
app.TurbEditField.Value=Turb;
end
end
Ok so I tried this. Thinking I am using properties in the wrong way though. Getting this error:
Index exceeds the number of array elements (0).
Error in test/ButtonValueChanged (line 80)
Evap=app.Myeq(1);
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 429)
Error while evaluating StateButton PrivateValueChangedFcn.
The error message would suggest that app.Myeq has not yet been assigned a value. I would expect that to be the case if either the ButtonValueChanged has not yet been run and you are trying to access Myeq somewhere else, or if none of your switch cases evaluate true.
Also, declare Myeq as a property like this
properties (Access = private)
Myeq % Equipment
end
If you want to preallocate with zeros, add that to a startupfcn perhaps like this
% Code that executes after component creation
function startupFcn(app)
app.eq = zeros(1,4);
end
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)
Just an additional note that you can also preallocate your property this way:
properties (Access = private)
Myeq = zeros(1,4); % Description
end

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2023년 4월 20일

댓글:

2023년 4월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by