How to pass arrays to and from a GUI?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am relatively new to MATLAB and just starting to try to use GUIs. Maybe I'm going about this all wrong, but I want to run a GUI from a MATLAB program. I want to pass arrays of X and Y values to the GUI, to plot and manipulate the data in the GUI, and then return the manipulated X and Y values plus certain other variables that are generated in the GUI back to the program. I cannot for the life of me figure out how to pass arrays and other variables between the program and the GUI. What am I missing?
1 Kommentar
Image Analyst
am 27 Okt. 2011
Do you want to pass variables just when you call up the GUI for the first time, or also after you've already call it up? Because it makes a difference. If it's just when you call it, you can do it via the arg list.
Antworten (8)
Image Analyst
am 27 Okt. 2011
Eric: Just pass them in via the argument list when you call it in your main routine:
test_2(mainVal1, mainVal2, mainVal3, mainVal4); % or whatever
Inside your test_2's OpeningFcn function, just extract them from the varargin:
function test_2_OpeningFcn(hObject, eventdata, handles, varargin)
guiVal1 = varargin{1};
guiVal2 = varargin{2};
guiVal3 = varargin{3};
guiVal4 = varargin{4};
Then use the link Walter gave you to learn how to share those values amongst the other functions in your GUI file that may need to use them. You might also look into the deal() function.
3 Kommentare
John
am 23 Jul. 2013
I want to show an image within my GUI. I am using GUIDE, so I created an axis where the image can go. Then I created a "CreateFcn" for that axis, which is where I am putting my imshow command. However, that function seems to be executing before my GUI's OpeningFcn. So, although I set one of my varargin values to handles.I within the OpeningFcn, it happens after my imshow(handles.I) within my CreateFcn. Any suggestions? Is the CreateFcn for my axis the wrong function to have my imshow(handles.I)?
Image Analyst
am 24 Jul. 2013
No, that's not the right place. Put it in the OepnFcn function for your GUI, or else in a callback function of a listbox or push button.
Naz
am 27 Okt. 2011
This is how I do it:
x=evalin('base','X');
where x is my variable in GUI, 'base' assumes WorkSpace and X is a variable that will be read from workspace to the GUI. Similarly, you can pass the variable to the workspace:
assignin('base', 'X', x)
where x is the GUI variable and X is a future variable in workspace.
0 Kommentare
Alex
am 26 Okt. 2011
I've started using a class based system to implement Gui's and passing information between.
The following little class relies on an existing GUI with a single button and text box. Each press of the button increments the value in the text box by 1. The data is being passed is an illusion because the class actually contains everything from the beginning.
The closerequest function is used so that if the gui is closed before the memory is cleared, the class is cleaned up.
classdef myclass < handle
%class properties that will eb shared over the entire class - these cannot be seen or changed from outside of the class
properties (Access = Private)
%pointer to the gui
gui_handle = [];
%generic counter
my_int = 0;
end
methods
%class constuctor
function this = myclass()
%create the gui and store the handle
this.gui_handle = guihandles("name of gui");
%set the callback fcn
set(this.gui_handle.button, 'callback', @(src, event) Btn_callback (this, src, event));
%set the close-request function so the class gets a signal when the gui closes
set(this.gui_handle.figure1, 'closerequestfcn', @(src, event) Close_fcn (this, src, event));
end
%class deconstructor
function delete(this)
%gui exists
if(isempty(this.gui_handle))
set(this.gui_handle.figure1, 'closerequestfcn', '');
delete(this.gui_handle.figure1);
this.gui_handle = [];
end
function Close_fcn(this, src, event)
delete(this);
end
function this = Btn_callbakc(this, src, event)
this.my_int = this.my_int + 1;
set(this.gui_h.my_text_box, 'string', sprintf('%i',this.my_int));
end
end
0 Kommentare
Walter Roberson
am 27 Okt. 2011
In slider1_Callback instead of your current lines
x2 = handles.x;
y2 = handles.y2;
save('output.mat','x2','y2','factor');
substitute
guidata(hObject,handles);
In your main program, in place of your current lines
test_2;
substitute
test2fig = test_2;
Then after the pause line that follows, for the "load" line, substitute
handles = uidata(test2fig);
and then extract handles.y2 and so on.
My main concern is that you have not defined a distinct "I'm done" in test_2, so the pause() that you use is not likely to work properly. Remember, you slider callback will stay active and will update the values many times over the course of a single slider drag. If there is no way for the user to signal they are done, the main routine may attempt to continue before anything has been done, or may sit waiting forever (or until the user closes test_2, but that would be a bad idea all of the data is being dangled off of something that is deleted if test_2 is closed.)
0 Kommentare
Eric H.
am 27 Okt. 2011
1 Kommentar
Walter Roberson
am 28 Okt. 2011
When you change the handles structure in a callback, you should use guidata(hObject,handles) to cause the global copy to be updated. Then, at any point in anyother routine where you expect that the handles structure might have been updated since the last time you looked at it, you need to use
handles = guidata(hObject);
in order to pull in the updated version.
Alex
am 28 Okt. 2011
I think the problem you are talking about is with the function call to "guidata(test_gui)".
This function is not a pass by reference, as in it does not keep a constant updated value of the data.
I ran your code (with a quick gui I made myself) and did the following. 1. created the gui. 2. ran g = guidata cmd - the result had no z data 3. adjusted the bar - the graph updated 4. looked at the variable g, containing the guidata - no z data. 5. ran g = guidata again, now the z data shows up.
What are you trying to specifically accomplish with this example? The factor and z data variables are not used but within the factor_slider callback, and therefore are not needed to be recorded.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!