Call OpeningFcn each time a button is pressed

11 ビュー (過去 30 日間)
Simon Aldworth
Simon Aldworth 2018 年 10 月 23 日
コメント済み: Rik 2018 年 10 月 24 日
Hi,
I have a piece of main code in which I call a GUI. In the GUI, the OpeningFcn function contains some code and calls uiwait:
handles.output = hObject;
% Set handles fields with initial values from UIcontrols
handles.speedrange = str2double(get(handles.editSpeedRange,'String'));
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes pitch_bounce_input_gui wait for user response (see UIRESUME)
uiwait(handles.bounce_pitch_fig);
The user can change a series of edit box values, which are all captured in the handles structure. When ready, a Run button is pressed that contains the uiresume line:
function pushRun_Callback(hObject, eventdata, handles)
% hObject handle to pushRun (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% resume code
uiresume(gcbf);
This executes the code in OutputFcn, which is some variable storage and manipulation, and returns to the main code. The main code runs a Simulink model in a loop, calculating a set of metrics from each run, and then produces a series of plots.
I would then like the user to have the option to press the Run button again, perhaps having adjusted some of the edit box values. However, the callback for the Run button just contains the uiresume command, which is now not paired to a uiwait. How do I call the OpeningFcn function again when the GUI has remained open (as I would like it to be)?
I haven't found reference to this problem anywhere so I'm concerned that it is less about finding the right code than correctly structuring it in the first place.
Thanks,
Simon.
  3 件のコメント
Simon Aldworth
Simon Aldworth 2018 年 10 月 23 日
Hi Rik,
So should I put my handles updates in the pausing function and not in OpeningFcn?
I'm afraid I don't follow the second part of your answer, can you provide some more detail please (I'm new to Matlab)?
Regards.
Guillaume
Guillaume 2018 年 10 月 23 日
I've not particularly tried to understand the problem here as I'm a bit short of time however my initial reaction on reading the question title was: don't do this.
The OpeningFcn is the function that is run when the figure is opened. Anybody reading the code will understand that it is its purpose. They will not expect that the function is going to run again just for a button press.
It is perfectly fine for the OpeningFcn and the button callback to both call the same function that will perform the same task on figure opening and button press but that common function shouldn't be call OpeningFcn. Don't surprise your reader.

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

採用された回答

Rik
Rik 2018 年 10 月 23 日
There are two strategies I described:
  1. Have a function that you call at the end of OpeningFcn. That function only call uiwait, so it can also be run from other callbacks, making it easier to have a pause button.
  2. Don't use uiwait to pause your loop, but use a check at the start of your loop. The state of this check can be set and loaded with the getappdata function. Your pausing function would then use setappdata(gcbf,'BreakLoop',true) and inside your (infite?) loop you can use if getappdata(gcbf,'BreakLoop'),break,end. (Don't forget to set it back to false when you restart the loop.)
  15 件のコメント
Simon Aldworth
Simon Aldworth 2018 年 10 月 24 日
I am following your advice, and am grateful for it. Don't be frustrated as you are helping fantastically - obviously more than you realise.
I am new to this so don't take my proposed structure as a given - it could all fall over as I begin to develop it.
However, to answer your point: yes, I want to pause the code, but I don't want the user to be responsible for that. The user will only have a run button. After executing the calculations the code will pause itself. This is as I explained it above.
Regards.
Rik
Rik 2018 年 10 月 24 日
In your other thread I think we have identified the root cause of the premature exit, so I'll ignore it here.
You can have the pause automatically trigger, but I would suggest you first make sure that your code runs without error when you separate the code that should run after the resume from the rest. That way you can simply call that code if you want to re-run the analysis with the same or with changed parameters.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by