Functions in gui script not seeing handles struct

조회 수: 3(최근 30일)
Hi,
I have produced a gui using GUIDE in which I add several fields to the handles struct for use later on (within the gui script).
I had two pieces of code that I stored in separate scripts (.m files) and these were called by several of the callbacks within the gui script. To call each of the separate scripts I just wrote the name of the script with parenthesis on the end:
function pitch_bounce_input_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to pitch_bounce_input_gui (see VARARGIN)
% Choose default command line output for pitch_bounce_input_gui
handles.output = hObject; % not required?
% Set handles fields with initial values from UIcontrols
handles.mass = str2double(get(handles.editMassNom,'String'));
% FURTHER CODE REMOVED FOR BREVITY
% Update handles structure
guidata(hObject, handles);
% Calculate characteristic values and update gui
calc_characteristics();
update_gui();
This worked perfectly - noting that I did not need to pass anything (i.e. handles) to the scripts for them to work. I'm not experienced enough to know why this is but was happy that it worked.
I have now chosen to incorporate those separate scripts as functions within the gui script file and now they don't work. The functions are written as:
%%A function to calculate the characteristic values
function calc_characteristics()
cgx =((((handles.frload/100)*handles.mass)/handles.mass)*handles.wb)-(handles.wb/2);
ryy = sqrt(handles.pinertia/handles.mass);
a = (1-(handles.frload/100))*handles.wb;
b = handles.wb-a;
alpha = (2*handles.frrate+2*handles.rrrate)/handles.mass;
beta = (2*handles.rrrate*b-2*handles.frrate*a)/handles.mass;
gamma = (2*handles.frrate*(a^2)+2*handles.rrrate*(b^2))/(handles.mass*ryy^2);
handles.bfreq = sqrt(((alpha+gamma)/2)-sqrt(((alpha-gamma)^2)/4+(beta^2/ryy^2)))/(2*pi);
% FURTHER CODE REMOVED FOR BREVITY
% pass back characteristic values
guidata(handles.bounce_pitch_fig, handles);
My first thought was that for some reason I know needed to pass handles to the functions, calling them as follows:
calc_characteristics(handles);
update_gui(handles);
And receiving them as:
function calc_characteristics(handles)
function update_gui(handles)
This worked for the first call but not the second. When I broke the script and continued line by line it was apparent that the contents of handles was growing correctly during the first function (to 116 fields) and then when the function ended and passed back to OpeningFcn the new content of handles was lost (back down to 98 fields!). This is despite using guidata to endure the content of handles was retained.
Why do I know need to pass handles explicitly when the function would seem to be closer to the gui script scope than it was previously? And why is it not storing the output of the first function correctly?
Also, as in a previous post of mine, the gui flashes up when it is called:
pitch_bounce_input_gui;
and then instantly disappears before getting to the first line of OpeningFcn.
These two problems are presumably related.
Thanks,
Simon.

채택된 답변

Walter Roberson
Walter Roberson 2018년 10월 29일
calc_characteristics(handles);
handles = guidata(hObject) ;
update_gui(handles);
Remember that when you update handles inside the calc_characteristics function, you are not updating the copy that lives in the workspace of pitch_bounce_gui_data
  댓글 수: 4
Simon Aldworth
Simon Aldworth 2018년 10월 31일
That's terrific Walter, many thanks indeed for taking the time to explain that.
Regards.

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

추가 답변(0개)

범주

Find more on Migrate GUIDE Apps in Help Center and File Exchange

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by