Matlab jumps over my if-statement in my GUI even if the value is true?
2 次查看(过去 30 天)
显示 更早的评论
Below is the snippet of the code. I was debugging and saw that the value for 'n' was 3 and 'm' was 1, thus it entered the initial if-statement. However, after executing the 2nd line it goes to the "if m==1" and instead of executing it (since it is true) it just skips to the second end. This is within my push-button callback.
n=getappdata(0,'n'); m=getappdata(0,'m');
if n>=1
cvList((n+1):12)=0;
if m==1
mList(2:12)=0;
elseif m==0
mList((n+1):12)=0;
end
end
11 个评论
Dennis
2018-8-1
编辑:Dennis
2018-8-1
Do you always edit each value of each CV MOPD field when you run your programm?
If not the callbacks of your edit fields won't get executed. The values then might appear random because some of them might been stored and retrieved from root of an earlier run.
A small example how to create a few edit boxes and change their visibility:
handles.fig=figure('position',[200 400 800 600]);
for i=1:12
handles.CV(i)=uicontrol('style','edit','string','0','position',[80 40+40*i 100 20],'visible','off'); %creating edit boxes and assigning handles
end
pause(2)
set(handles.CV,'callback',{@mycb,handles})
set(handles.CV,'visible','on') %change all edit boxes
function mycb(hObject,~,handles) %1 callback to rule them all =P
myvalue=getappdata(handles.fig,'myvalue'); %data stored at handles.fig rather than root
if isempty(myvalue)
myvalue.CV=zeros(12,1); %data stored as 1 structure instead of dozens of variables
end
for i=1:12
if hObject == handles.CV(i) %check which edit box issued the callback
myvalue.CV(i)=str2double(get(hObject,'String'))
end
end
setappdata(handles.fig,'myvalue',myvalue)
myvalue.CV
end
Stephen23
2018-8-1
编辑:Stephen23
2018-8-1
"How exactly can I access all the objects at the same time?"
Commands set and get work with arrays of handles so you don't need to do anything special, e.g. to set all objects in array A to invisible:
set(A,'Visible','off')
You can even set different values for an array of handles, so they don't all need to use the same value: read the documentation carefully to know how, and experiment!
"I understand I can place their values as such:"
handles.CV{1}=get(hObject,'Value');
"But how can I store them in array format to access their visibility? Since they are all different edit boxes?"
The handles should be stored in an array. This is easy using indexing (e.g. creating the boxes in a loop) or all at once (many graphics commands can create multiple objects at once, and return an array of handles). The fact that they are different edit boxes is irrelevant. If the boxes are arranged systematically then they could easily call the same callback with an extra argument (e.g. the loop counter) and then the callback function knows which button was pressed or the counter can be used as an index. See some of my FEX submissions for examples of this:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!