How do I pass a value out of a GUI when button is pressed? The value must be available to another m file

Hello,
I am making a GUI which adds rows to a matrix in one part. I want the matrix to be available to another m-file when I press another button (export). I've been looking at this for two weeks and can't figure it out. What is the syntax to make this matrix available to another function? eg x = function (y, exported_matrix). The normal [export_matrix] =Callback_pushbutton(....) doesn't work for GUI's. Can someone explain this? Sorry, this is probably basic but I can't see by any examples how this would work with my own GUI.
Thanks,
Brian

 採用された回答

Brian
Brian 2013 年 6 月 25 日
For reference to anyone with the same problem: Use global. It may or not be bad programming practivce but it works. global has to be stated in BOTH m files.
eg.
m-file 1
global BND_CDN
...
in m-file 2:
global BND_CDN
a = BND_CDN etc.

1 件のコメント

You had said "I have tried global, ..., I have been at this for three days, I'm not getting it. " I guess you eventually got it right.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 5 月 26 日

7 件のコメント

Thanks for your reply. I have, I'm not understanding it even though I have read it a load of times. For a test, to pass variables between GUI's I have the following code in the first GUI (one of the syntax's I have tried):
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
region = 1;
Loadx = 1;
Loady = 3;
Suppx = 3;
Suppy = 4;
BND_CDN = zeros(100,5);
BND_CDN(region,1:5) = [region, Loadx, Loady, Suppx, Suppy]
BND_CDN = handles.pushbutton1;
% BND_CDN = getappdata(handles.GUIHandle , 'BND_CDN')
In the next GUI (these are just GUI's to try to pass a variable from one to the other by pushing a button):
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x = BND_CDN
The error then is: Error using feval Undefined function 'pushbutton1_Callback' for input arguments of type 'struct'.
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in test_3 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)test_3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Thanks for your help
The key line in the FAQ that you should be concerned with is this: "To have GUI1 control GUI2 when GUI2 is already running, you can use the assignin() function." Did you try to use assignin()? It doesn't look like it.
Brian
Brian 2013 年 6 月 21 日
編集済み: Brian 2013 年 6 月 21 日
Thanks for getting back to me. Sorry for the delay, I was away. I had tried assignin(), not properly. Can you explain why rhe variable 'BND_CDN' is undefined
In test_2 (the gui passing the variable to the other GUI, test_3) the code is:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc
region = 1;
Loadx = 1;
Loady = 3;
Suppx = 3;
Suppy = 4;
BND_CDN = zeros(100,5);
BND_CDN(region,1:5) = [region, Loadx, Loady, Suppx, Suppy];
assignin(test_3, 'BND_CDN', BND_CDN)
There are no errors reported for this. In test_3, (the GUI that should receive the variable, as a test to see if it would receive BND_CDN, I placed BND_CDN in the code to see if it would be printed in the workspace (the function it belongs to is part of the GUI)
function test_3_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes test_3 wait for user response (see UIRESUME) % uiwait(handles.figure1);
BND_CDN
The error is: Undefined function or variable 'BND_CDN'.
Error in test_3>test_3_OpeningFcn (line 58) BND_CDN. Should that not have passed to test_3? Thanks again for your time
Brian
Brian 2013 年 6 月 21 日
PS: Apologies for asking you to explain to this level. I have tried global, persistent, userdata functions, this is only one of my many tries, I have been at this for three days, I'm not getting it. Thanks for your help
Jan
Jan 2013 年 6 月 21 日
"assignin(test_3, 'BND_CDN', BND_CDN)" should fail, because "test_3" will most likely not reply one of the two allowed strings 'base' or 'caller' - look into the documentation to find out more: doc assignin.
Assigning variables remotely and magically using ASSIGNIN or EVALIN increases the complexity of a program and decreases the processing speed. So it is recommended to avoid it strictly, especially for beginners. And to be absolutely clear in this point: Don't do this!
Brian
Brian 2013 年 6 月 22 日
I'll try to stay away from that, as I am a beginner. I'll try using handles. Cheers
Brian
Brian 2013 年 6 月 25 日
Thanks for your time, I couldn't get it to work. I needed a simple answer, global did it, I didn't understand that global had to be in both files

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

Jan
Jan 2013 年 6 月 21 日
編集済み: Jan 2013 年 6 月 21 日
Assume your GUI has the tag-property 'MyGUI' (a more meaningful tag is recommended...). Then the store the matrix in the handles struct inside the GUI:
handles.matrix = rand(10, 10); % Or how ever you define the elements
guidata(hObject, handles); % 1st argument: handle of the current object
Now obtain the matrix from your external M-file:
function theExternalFunction
guiHandle = findobj(allchild(0), 'flat', 'tag', 'MyGUI');
guiHandles = guidata(guiHandle);
matrix = guiHandles.matrix;
...
guidata() is a wrapper for setappdata() and getappdata(). Some exceptions should be caught by tests, e.g. if the guiHandle cannot be found because the GUI has been closed before, etc.

2 件のコメント

Thanks for your help. I still can't work it though. The output when I press the button in the GUI seems to be working, the output with colons removed is:
handles =
figure1: 173.0012
pushbutton1: 174.0012
output: 173.0012
bnd: [100x5 double]
As testing, I copied your code and slightly changed it:
function theExternalFunction
guiHandle = findobj(allchild(0), 'flat', 'tag', 'test_2')
guiHandles = guidata(guiHandle)
BND_CDN = guiHandles.BND_CDN
The output in the command window is then
guiHandle =
Empty matrix: 0-by-1
Error using guidata (line 89)
H must be the handle to a figure or figure descendent.
Error in test_theExternalFunction (line 3)
guiHandles = guidata(guiHandle)
Should the guiHandle matrix be populated? Do you know what the problem is? Thanks again
Brian
Brian 2013 年 6 月 25 日
Thanks for your time, I couldn't get it to work. I needed a simple answer, global did it, I didn't understand that global had to be in both files

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

カテゴリ

ヘルプ センター および File ExchangeCOM Component Integration についてさらに検索

質問済み:

2013 年 5 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by