How to obtain information from handle structure?

Hi everybody, I have following situation
function outParameters = Main(inParameters)
...
Doing something
...
h = figure(KeyPressFCN,@WhatPressed);
plot(WhatNecessary);
...
function WhatPressed(src, evnt)
switch evnt.Key
case 'a'
...
ProcessedData
case 'b'
...
end
end % of WhatPressed
outParameters = workWith(ProcessedData);
end % of Main
In above version I calculated ProcessedData, but can't pass it into Main program.
What is the simplest way to pass result of calculations from WhatPressed function into Main one?
I know the way how to do this, using GUI with main window and processing of its handle structure and I use it for control of many operations, but in this case I need only to pass a few parameters from WhatPressed function into Main one?
Thanks for constructive ideas

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Jan. 2018

0 Stimmen

7 Kommentare

Thank you Walter Robertson for your quick answer.
In fact I know this text, but it not helps me. I know how to pass information between two GUIs.
My question is how to create handle structure, to which will be passed results of calculations before following strings:
h = figure(KeyPressFCN,@WhatPressed);
plot(...)
I have already try to annonce result of calculation as global, the same with assignin(), but it not works. Could you give me idea, how to solve this issue?
Valeriy
Valeriy am 20 Jan. 2018
Right now I made experiments with uicontrol, but I'm not sure that this is correct way.
curfig = gcf;
handles = struct('ProcessedData', []);
guidata(curfig, handles);
newfig = figure('KeyPressFcn', {@WhatPressed, curfig});
uiwait(newfig); %wait for user to interact to completion
handles = guidata(curfig);
ProcessedData = handles.ProcessedData;
outParameters = workWith(ProcessedData);
...
function WhatPressed(src, evnt, masterfig)
...
ProcessedData = ...;
...
handles = guidata(masterfig);
handles.ProcessedData = ProcessedData;
guidata(masterfig, handles);
...
Using guidata is not required but it is well understood. You could also write it in terms of appdata:
curfig = gcf;
setappdata(curfig, 'ProcessedData', []);
newfig = figure('KeypressFcn', {@WhatPressed, curfig});
uiwait(newfig);
ProcessedData = getappdata(curfig, 'ProcessedData');
outParameters = workWith(ProcessedData);
...
function WhatPressed(src, evnt, masterfig);
...
ProcessedData = ...
...
setappdata(masterfig, 'ProcessedData', ProcessedData);
...
Valeriy
Valeriy am 20 Jan. 2018
Dear Walter,
thanks a lot! This is what I was searching about.
Key information for me is using additional figure (curfig) to produce handle structure and using of uiwait(newfig) command. Without last one I can't go out from the space of WhatPressed.
Your help is very appreciated and efficient, thanks again
Valeriy
You do not need an additional figure. curfig can be any figure that already exists and will outlast the new figure. And if no such figure exists then you can use the setappdata version against groot (or against 0 if you are using R2014a or earlier.) In my setappdata version just change curfig = gcf; to curfig = groot;
The uiwait() is important unless the new figure you create is marked as 'WindowStyle', 'modal' . Non-modal windows (the default) act independently and no action might have occurred by the time you went on to try to use the data.
Valeriy
Valeriy am 20 Jan. 2018
Excellent, you have moved me further in understanding all of this staff.
Should I use uiresume(newfig) during the exit part of WhatPressed?
Appreciate your efficient help
If WhatPressed is being closed then you do not need uiresume() as uiwait() will detect that the figure closed. uiwait() / uiresume() could be used in a loop in the main function for the GUI to communicate some information back to the main routine without exiting -- uiresume() can be used to signal "There is data ready for you" without needing to close the figure doing the data collection. But if you are closing the figure anyhow then you do not need to bother to uiresume()
If you have a figure used for data interaction that is going to be used multiple times and should not be available in-between, then instead of creating the interaction figure each time, you can arrange to have it uiresume() and then set itself invisible; when you need it again, set its visibility on and uiwait() on it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Valeriy
Valeriy am 21 Jan. 2018

0 Stimmen

Dear Walter
I have realized workflow you have proposed, it works excellent. I have to tell you that this was not the first time I try to solve such task. I carefully searched help, forums, other publications. I found how to pass additional parameters into space of figure, but your post is first realization of inverse transfer of information. I'm sure that this solution is useful not only for me, but for many peoples who have similar tasks. By this reason in could be presented into regular matlab help.
By the way, for the case of appdata and earlier Matlab versions, should I replace all occurences of curfig by 0?
Thanks again for your efficient help

1 Kommentar

Easier to assign 0 to curfig to isolate the effects. Or use a more meaningful variable name.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by