简体   繁体   中英

For Loop within Matlab GUIDE

I have Matlab GUI that uses push button that executes certain code. all of that works, except that when code reaches for loop, execution does not seem to enter that.

1) Any idea how for loop is implemented within framework of Matlab GUIDE code?
2) is there a way to debug GUI code ( only way I could was trough 'disp' statements,) using breakpoints, as when I run the GUI, breakpoints get removed.

after having read the solutions, I found that
(1) happened because the for loop index was not changing as length of array that I was indexing on, was not changing. I explain this in the code.
(2) My bad, I was putting breakpoints before running the GUI, when I did the other way around, it breaks fine.

% Opening func
function Regression_OpeningFcn(hObject, ~, handles, varargin)
NoiseMin = -12;
NoiseMax = 10;
NoiseRes = 2;
handles.noiseMin = NoiseMin;
handles.noiseMax = NoiseMax;
handles.noiseRes = NoiseRes;

%**this executed when value changed in edit text box***

function noiseMinDbEditText_Callback(~, ~, handles)
handles.noiseMin = str2num(get(handles.noiseMinDbEditText,'String'));

% When GUI is running, following shows change from -12 sucessfully 
disp(strcat('Noise Min = ',num2str(handles.noiseMin)));

function noiseMinDbEditText_CreateFcn(hObject, ~, ~) 
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
set(hObject,'BackgroundColor','white');
end

 %Similar code for noiseMax and noiseRes(not shown here)  

`% Here, code enters the Pushbutton callback

function StartRegressionPushButton_Callback(~, ~, handles)
snr_res = handles.noiseRes;
% here, snr_vecs still shows [-12 2 10]
snr_vecs = [handles.noiseMin:handles.noiseRes:handles.noiseMax];

So, basically, when GUI runs, entering and changing the values in text box corresponding to min, max and res snr variables shows me the change, but as soon as I enter the pushbutton dialog box, the changed values( that were captured in global variable "handles") do not show up as I break in the code there.
Any help with this? [for loop problem arose as I was indexing based on snr_vecs array, which is not changing, and so for loop was executing,but not as it should be]
sedy

Using Matlab guide

Guide is basically only a tool to create fig. files. All you can do with guide you could do yourself programmatically. Creating ui-elements works by hand just as easy as with guide (I actually prefere creating gui elements programmatically, since I think guide is very poorly coded)...

Every guide-elements has its callbacks which have to be coded somewhere, usually (I think 100% of the time) the fig file has the same name as the .m file. Find the corresponding .m -file and go to the callback you are interessted in. There you can place breakpoints just as easy as in any other piece of code.

Note: you can even change the code without having to reopen the fig file!

For loops or anything that works in regular code works for ui-element code.

The reason for handles not getting updated was missing following statement at end of function callback whose variable needs to be used in other callback.

guidata(hObject,handles);

I fixed that and it works nicely.

sedy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM