Info
This question is closed. Reopen it to edit or answer.
How to get to work a function created in another .m file using GUI
1 view (last 30 days)
Show older comments
I have created two .m files using GUIDE. In one file, which is called well_log_selection.m, has the following function:
function [dtco,md]=pushbutton3_Callback(hObject, eventdata, handles)
Loading_Well_logs
dtco=str2double(get(handles.edit3,'String'));
md=str2double(get(handles.edit2,'String'));
matrix=pushbutton1_Callback(hObject, eventdata, handles);
In the other file, called Loading_Well_logs.m, I have the following function:
function Loading_Well_logs_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
[dtco,md]=pushbutton3_Callback(hObject, eventdata, handles);
x=matrix{dtco};
y=matrix{md};
I am getting the following error:
Undefined function or variable 'pushbutton3_Callback'.
Error in Loading_Well_logs_Loading_Well_logs_OpeningFcn (line 63)
[dtco,md]=pushbutton3_Callback(hObject, eventdata, handles);
Error in gui_mainfcn (line 220)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in Loading_Well_logs (line 42)
gui_mainfcn(gui_State, varargin{:});
My problem is that the function [dtco,md]=pushbutton3_Callback(hObject, eventdata, handles) is not recognized by the Loading_Well_logs.m file, how can I fix this problem?
Thanks in advance
0 Comments
Answers (2)
Walter Roberson
on 4 Feb 2020
You cannot fix this in any direct way.
Any function similar to pushbutton3_Callback is a local function to well_log_selection and can only be called from outside well_log_selection if well_log_selection deliberately makes a function handle available for it.
If you had the handle to the well_log_selection figure then you could access the field named pushbutton3 and get the resulting objects Callback property. That would be the handle to an anonymous function that calls pushbutton3_Callback, something similar to
@(hObject, event) pushbutton3_Callback(hObject, event, guihandles(hObject))
This gives you a handle to a function that can be used to call the callback, provided that you appropriately manipuate the hObject to be that belongs to the well_log_selection figure, and provided that you fill in appropriate event data.
If you did not add a waitfor() or uiwait() to the well_log_selection OpenFcn (it exists in commented out form by default), then another approach is to call well_log_selection passing in 'pusbutton3_Callback' and appropriate parameters, and well_log_selection will make the call on your behalf.
A better way to arrange all of this is to rewrite well_log_selection pusbutton3_Callback to do almost nothing except to call a function that you store in its own .m file. If you do that, then you can call that function directly from Loading_Well_logs
0 Comments
leydy Garcia
on 4 Feb 2020
Edited: leydy Garcia
on 4 Feb 2020
2 Comments
Walter Roberson
on 4 Feb 2020
What you could do is
handles.pushbutton3_Callback = @pushbutton3_Callback;
guidata(handles)
inside well_log_selection's OpenFcn . If you did that, then in the other gui once you knew the handle to the well_log_selection figure:
well_log_selection_figure = some appropriate code to get the handle to well_log_selection figure
wls_handles = guidata(well_log_selection_figure);
pb3c = wls_handles.pushbutton3_Callback;
fake_event_data = something appropriate
pb3c(wls_handles.pushbutton3, fake_event_data, wls_handles);
But this is a bunch of trouble to go to when it would be so much easier to put all of the actual work into a function that you could call from both places.
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!