nested function / Pass Data between 2 functions

1 回表示 (過去 30 日間)
Max Müller
Max Müller 2014 年 8 月 12 日
回答済み: Max Müller 2014 年 8 月 19 日
So this is my exampple Code. Now, i want to know: how can i pass the variable "Number" from the 1st function into the 2nd one. (hope pass is the right word)
function Example_Callback(hObject, eventdata, handles)
Number = get(handles.edit1,'String')
Data = findobj('type','axes')
set(Data,'buttondownfcn',@ClickToDrag)
function ClickToDrag (gcbo,evendata,handles)
disp('Number')

採用された回答

Max Müller
Max Müller 2014 年 8 月 19 日
1 Comment Adam on 13 Aug 2014 at 12:07
>>> I always forget exactly what are the fixed arguments you have to have for a callback function, but I'm pretty sure your Click function shouldn't take 'handles' as an argument. If it takes an argument representing 'A' (which may well end up in handles the way you have it) then that should be simply usable in your callback.
e.g.
function Click( gcbo, eventdata, A )

その他の回答 (4 件)

Adam
Adam 2014 年 8 月 12 日
set(Data,'buttondownfcn',{@ClickToDrag,Number})
should do the job.
Incidentally, is 'handles' appearing in your ClickToDrag function because you aren't passing it to the function. I would also never advise passing handles to a callback function. Instead pass the handle of your main figure and get the handles from that within your callback. That will give you the handles structure at the time of the callback. If you pass handles to a callback you will get the version of handles at the time it was passed.
  1 件のコメント
Max Müller
Max Müller 2014 年 8 月 13 日
and this doesn't work either.....

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


Joakim Magnusson
Joakim Magnusson 2014 年 8 月 12 日
You could store it in handles like this in the first function:
handles.number = Number;
Then you could use it in your second function like this:
disp(handles.number);
  2 件のコメント
Joakim Magnusson
Joakim Magnusson 2014 年 8 月 12 日
Did you solve it?
Max Müller
Max Müller 2014 年 8 月 13 日
that doesnt quite work.

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


Max Müller
Max Müller 2014 年 8 月 13 日
I am forced to use v2006a
I have a solution using getappdata|setappdata.
function pushbutton2_Callback(hObject, eventdata, handles)
A = 5
Data = findobj('type','axes');
set(Data(1),'buttondownfcn',{@Click,A});
setappdata(Data(1), 'my_A', A);
function Click (gcbo,evendata,handles)
disp('Click')
Data = findobj('type','axes');
A = getappdata(gcbo, 'my_A');
disp(['A = ' A])
  1 件のコメント
Adam
Adam 2014 年 8 月 13 日
I always forget exactly what are the fixed arguments you have to have for a callback function, but I'm pretty sure your Click function shouldn't take 'handles' as an argument. If it takes an argument representing 'A' (which may well end up in handles the way you have it) then that should be simply usable in your callback.
e.g.
function Click( gcbo, eventdata, A )

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


Max Müller
Max Müller 2014 年 8 月 14 日
Sorry for being such an idiot. My Job isnt easy .... Today i try to work on my knowledge about handles. But guys, is there anyway my CallbackFunction can return Data to my parentFunction ? my Supervisor said no. What do u say ?
  2 件のコメント
Adam
Adam 2014 年 8 月 14 日
編集済み: Adam 2014 年 8 月 14 日
A callback does not have a parent function, but you can attach data in your callback to the handles structure that can be accessed from any other callback.
handles = guidata( gcbo );
handles.myData = someData;
guidata( gcbo, handles );
if you have gcbo or the main figure handle in a standard callback that comes from a pushbutton or similar. I tend to pass the main figure handle to my created callbacks so I can access the handles structure from that.
Make sure not to forget that last line though. I still forget it from time to time after 8 years using it and if you don't then your handles will not get updated to the gui.
Max Müller
Max Müller 2014 年 8 月 14 日
thanks a lot. I am trying to learn more about matlab handles right now. Hope can become a better matlab programmer.

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

カテゴリ

Help Center および File ExchangeDialog Boxes についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by