How to transfer data from one GUI to a uitable of another GUI

3 次查看(过去 30 天)
i have a main gui with a push button and a uitable. when i click the pushbutton it will open another GUI that contains edit boxes and a pushbutton. The idea is that when i fill out the edit boxes and click the pushbutton of the second GUI it will transfer the generated result/data back to the uitable of the main GUI.
I hope this clarifies the problem. thanks

采纳的回答

Adam
Adam 2016-12-2
编辑:Adam 2016-12-7
There are a few different methods. This is a very very rough sketch of the setup I use. I have answered a few questions similar to this saying that OOP is my recommended solution, but that it is too complicated to explain for someone who doesn't know it, but today I figured I would try to give the most simple outline.
This is rushed off from the top of my head so apologies for any errors or omissions. If you wish to try to OOP approach I will try to answer any questions if you get stuck (or other OOP 'experts' can also - I'd certainly never consider myself an expert, but relatively experienced with OOP by now!).
is an excellent guide and I recommend using things like property access specifiers and set functions with validation etc to tighten up your code and reduce bugs, but that is all extra to the basic solution.
In a separate file define this class. The properties will be whatever you would call the variables that the data from your edit boxes will fill. I would recommend more meaningful names throughout of course.
classdef SecondGUIStuff < handle
events
StuffChanged
end
properties
param1;
param2;
...
end
end
Note the class derives from the 'handle' base class. This allows it to be passed by reference which is crucial and also to be able to fire off events that can be reacted to elsewhere.
In your first GUI, create an object of this class, store on handles, and in your pushbutton callback pass it as an argument to your second GUI. We also add a listener which will deal with the communication from the 2nd GUI:
In e.g. OpeningFcn:
handles.parameters = SecondGUIStuff;
handles.paramListener = addlistener( handles.parameters, 'StuffChanged', @( src, evt ) updateTable( handles.figure1 ) );
Note: figure1 is the default tag of your GUI in GUIDE. I always change this (to be the same as the GUI name) so if you have changed your GUI tag then change this accordingly.
In callback:
function pushbuttonCallback(...)
MySecondGUI( handles.parameters );
Then create the callback function that will update the table:
function updateTable( hGUI )
handles = guidata( hGUI );
% update your uitable here by accessing handles.parameters.param1 etc..
In MySecondGUI, store that input (varargin{1}) on handles and then in your edit box callbacks or when you click the push button on the second GUI, put the data from your edit boxes into the class e.g.
handles.parameters.param1 = get( handles.edit1, 'String' );
handles.parameters.param2 = get( handles.edit2, 'String' );
etc
Then at the end of the pushbutton callback call
handles.parameters.notify( 'StuffChanged' )
This line sends an event telling anything that is listening that something changed. Up above we added a listener in your first GUI which triggers a callback to update your table in response to an event change. Because the class used derives from handle and is passed by reference, the object stored in your first GUI and that in your 2nd GUI are the same so in your first GUI you have access to the parameters you just filled up in the 2nd GUI.
If you wanted to be able to also change these in the first GUI then you would need a listener in the 2nd GUI also to avoid it getting out of sync with the underlying object. Obviously the object can be changed programmatically too, it just depends how tight you want your control to be. Usually I don't handle this because changing of the parameters in that manner is not supported by my workflow.
  6 个评论
mark
mark 2016-12-7
awesome job, Adam. OOP is on my priority study list.This is definitely what i wanted to implement on a project I am working on. I'll put your name on the acknowledgements when it is completed. But for now I'll be asking some more questions when I get stuck..if you won't mind.
Thank you and have a happy holidays!
Adam
Adam 2016-12-7
No problem. My responses may be a little on and off since I just drop in to Matlab Answers for occasional breaks during my work while I let whatever I am working on sink in, but I will endeavour to respond when I can.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by