Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How to get to work a function created in another .m file using GUI

조회 수: 1 (최근 30일)
leydy Garcia
leydy Garcia 2020년 2월 4일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (2개)

Walter Roberson
Walter Roberson 2020년 2월 4일
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

leydy Garcia
leydy Garcia 2020년 2월 4일
편집: leydy Garcia 2020년 2월 4일
Thanks Walter!
would you create the handle in well_log_sellection.m file?
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 2월 4일
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.
leydy Garcia
leydy Garcia 2020년 2월 4일
it is a buch of trouble indeed. I would put all of the actual work into a function that I could call from both places. For this, I have to create the handle, right? but I am having some troubles here. In Loading_wells.m figure, I have added the function f, which will be calling the function created in Well_log_selection.m figure, am I right? but the variable dtco, for example, is not recognized. I should have been stored as it is a variable that comes from the bushbuttto3_Callback function.
function Loading_Well_logs_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 Loading_Well_logs (see VARARGIN)
% Choose default command line output for Loading_Well_logs
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Loading_Well_logs wait for user response (see UIRESUME)
uiwait(handles.figure1);
%[dtco,md]=pushbutton3_Callback(hObject, eventdata, handles);
f=@pushbutton3_Callback;
x=matrix{dtco};
y=matrix{md};
plot(handles.axes1,x,y);
the error is this:
Undefined function or variable 'dtco'.
Error in Loading_Well_logs>axes1_CreateFcn (line 180)
x=matrix{dtco};
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Loading_Well_logs (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Loading_Well_logs('axes1_CreateFcn',hObject,eventdata,guidata(hObject))
So, I am assuming my handle function f is wrong. What should I fix?

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by