Input argument "hObject" is undefined??

1 回表示 (過去 30 日間)
Omair
Omair 2012 年 5 月 26 日
Hi
I'm trying to figure out the whole handles structure thing. I created two simple functions in two separate m files and I want to see if I can pass data from one to the other using the handles structure. But I am getting an error message when I try to update the handles structure using guidata(hObject,handles)
I get the following error even before the second function is executed.
Input argument "hObject" is undefined
This is my code:
Function one
function fct1(hObject, eventdata, handles)
handles.x=5
handles.b=6
guidata(hObject,handles)
end
Function two
function fct2(hObject, eventdata, handles)
value1=handles.a
value2=handles.b
end

採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 26 日
You did not invoke fct1 with three arguments.
MATLAB automatically supplies hObject and eventdata as arguments to (nearly all) callbacks that are invoked as callbacks, but it does not automatically supply those parameters when you invoke the functions manually.
If you are doing this for testing purposes, test with
testgui = figure();
handles.x = -1;
handles.y = 'a test';
guidata(testgui, handles);
fct1(testgui, [], handles);
updatedhandles = guidata(testgui);
fct2(testgui, [], updatedhandles);
  2 件のコメント
Omair
Omair 2012 年 5 月 26 日
Thank you once more. I tried splitting these lines into two functions in two separate m files, however, I'm having trouble accessing the handles that were passed on to fct2.
It seems that the handles structure is still localized in the first function and cannot be accessed from the second.
My code is the following:
*First function*
fct1(testgui, eventdata, handles);
testgui = figure();
handles.x = -1;
handles.y = 'a test';
guidata(testgui, handles);
updatedhandles = guidata(testgui);
* Second function*
fct2(testgui, [], updatedhandles);
thenewhandles=updatedhandles;
Didn't work as I am getting that updatedhandles is not a recognized variable. Any help? :)
Walter Roberson
Walter Roberson 2012 年 5 月 26 日
The first file needs to have the fct1() call _after_ the guidata() call.
The second file should start from the "updatedhandles = " line
The one and only thing that needs to be passed between the first file and the second file is the testgui variable.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by