share some value between callbacks

1 view (last 30 days)
lily bentham
lily bentham on 21 Apr 2015
Answered: jagpreet makkad on 21 Apr 2015
i have four variables which two of them,named 'lat' and 'long' get value in a popup menu like this
case 1 long=48.2331; lat=30.3653;
...
case 2 long=48.7619; lat=31.3375;
.... case 3 long=49.84729; lat=34.13815;
.... case 4 long=48.421726199999966; lat=38.3236124; .... otherwise end guidata(hObject,handles)
and two others, named 'lat1' and 'long1', which get value in the same way in a different popup menu and this four, finally would be used in a third popup menu like this:
midl=(lat+lat1)/2; midl1=(long+long1)/2; midl2=(lat+midl1)/2; midl3=(long+midl1)/2; midl4=(lat1+midl)/2; midl5=(long1+midl1)/2;
first i tried global variables but didn't work at all, then i tried handles structures and read every thing about it and read everything about sharing data and value between functions and callbacks and every question in stack overflow around this topic but it didn't help me. i am new in mat lab so any help and suggestion would be appreciated. thanks in advanced .

Answers (2)

Adam
Adam on 21 Apr 2015
What do you mean by "i tried handles structures and read every thing about it and read everything about sharing data and value between functions and callbacks and every question in stack overflow around this topic but it didn't help me"? Using handles is, for me, by far the simplest method for doing this and the one I have used most of the last 7 or so years until I started doing some programmatic GUIs in OOP.
You should simply be able to put:
handles.lat = lat;
handles.long = long;
guidata( hObject, handles )
at the end of the callback function you post in your question (and the same for lat1 and long1, though I strongly recommend more meaningful names!).
Then in your callback in which you want to use this data you can either refer to handles.lat wherever you use it or simply put
lat = handles.lat;
long = handles.long;
at the start of your function if you use them multiple times and want a shorter variable name. Then just do your maths as normal.
Which aspect of that does not seem to be working for you?

jagpreet  makkad
jagpreet makkad on 21 Apr 2015
hi lily, In your callback function, you can assign the variables(which you want to use in some other function) in the base workspace using 'assignin' function and excess those variables from the base workspace whenever it is needed using 'evalin' function. Example: handle = evalin('base','handle');% for taking value of variable from base workspace
assignin('base','handle',handle);% for assgning the value of variable to base workspace

Categories

Find more on Code Execution in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!