How do I use a value of a pop-up menu (in the GUI) in an if-loop?

1 次查看(过去 30 天)
Hello,
i'm writing an interface with the GUI for an oscilloscope. I want to use the value of a pop-up menu in an if-loop to determine the scale of my oscilloscope.
this is the code i wrote:
contents = get(handles.popupmenu1,'String');
energyLevel = contents(get(handles.popupmenu1,'Value'))
if strcmp(energyLevel, '100') | strcmp(energyLevel, '150')
set(deviceObj.Channel(1), 'Scale', 0.2);
set(deviceObj.Channel(2), 'Scale', 0.2);
else set(deviceObj.Channel(1), 'Scale', 0.5);
set(deviceObj.Channel(2), 'Scale', 0.5);
Only it gives the following error: A nested function cannot be defined inside a control statement (if, while, for, switch or try-catch).
Be aware that in addition to this message, other messages might appear in the file. Such as, statement might not be aligned with its matching END and Parse error at END: usage might be invalid MATLAB syntax. When you remove the nested function definition from the control statement, these other messages might disappear
I am not familiar with this error. Can somebody help me?

回答(1 个)

Ingrid
Ingrid 2015-6-17
编辑:Ingrid 2015-6-17
are you not just missing an end or did you forgot to copy it?
energyLevel = str2double(contents(get(handles.popupmenu1,'String')))
if or(energyLevel==100,energyLevel==150)
set(deviceObj.Channel(1), 'Scale', 0.2);
set(deviceObj.Channel(2), 'Scale', 0.2);
else
set(deviceObj.Channel(1), 'Scale', 0.5);
set(deviceObj.Channel(2), 'Scale', 0.5);
end
  2 个评论
Pien_
Pien_ 2015-6-17
I forgot to copy! Oops.
However, even with your adapted code, it still gives the same error. When I try to run it is also says: Function definition is misplaced or improperly nested.
Ingrid
Ingrid 2015-6-17
I do not think the mistake is in this code but somewhere else in your file which is difficult to assess here

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by