필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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
Image Analyst
Image Analyst 2013년 6월 25일
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
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
Brian
Brian 2013년 6월 22일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by