如何在MATLAB GUI中的函数之间传递值? [英] how to pass value among functions in MATLAB GUI?
问题描述
我正在尝试将图像文件转换为二进制文件,在这里我使用Matlab GUI中的2个按钮. 第一个按钮浏览文件,然后在静态文本上显示文件名,第二个按钮将其保存为CSV文件,这是我的代码
I'm trying to convert an image file into a binary file, where I'm using 2 buttons in Matlab GUI. the first button it to browse the files then shows the file name on a static text, and the second button for saving it as a CSV file, and this is my code
function varargout = signatureGUI(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @signatureGUI_OpeningFcn, ...
'gui_OutputFcn', @signatureGUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before signatureGUI is made visible.
function signatureGUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.filename = 0;
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = signatureGUI_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in browsefile.
function browsefile_Callback(hObject, eventdata, handles)
filename = uigetfile({'*.jpg'; '*.jpeg'; '*.png'});
handles.filename=filename;
guidata(hObject, handles);
set(handles.showfile, 'String',filename );
% --- Executes on button press in buttonwritecsv.
function buttonwritecsv_Callback(hObject, eventdata, handles)
handles.filename;
new = imread (filename);
newrgb=rgb2gray(new);
newrgb_io = binz(newrgb);
[filecsv,pathcsv] = uiputfile('*.csv','Save CSV File');
csvwrite([filecsv,pathcsv], newrgb_io);
guidata(hObject, handles);
我试图使用"handles.filename"让另存为"按钮能够从浏览"按钮获取变量,但是当我浏览文件后单击另存为"按钮时,它什么也没做. ..
I'm trying to use the " handles.filename " to let the Save As button able to get the variable from the Browse button, but when I click on the Save As button after browse for the file, it do nothing...
我丢失了某些东西或在使用中犯了错误吗?
Am I missing something or making mistake in using it?
遵循@Richante的回答后,出现以下错误,我不理解
After I follow the answer from @Richante I got this following error, which I dont understand
??? Reference to non-existent field 'output'.
Error in ==> signatureGUI>signatureGUI_OutputFcn at 34
varargout{1} = handles.output;
Error in ==> gui_mainfcn at 265
feval(gui_State.gui_OutputFcn, gui_hFigure, [],
gui_Handles);
Error in ==> signatureGUI at 17
gui_mainfcn(gui_State, varargin{:});
我不知道这是什么.
推荐答案
是的,这不太正确.不要使用handles
存储数据-使用您自己的结构.在OpeningFcn
:
Yes, this isn't quite right. Don't use handles
to store the data - Use your own struct. In OpeningFcn
:
myData.filename = [];
guidata(hObject, myData);
然后,当您要检索此数据时(例如browsefile_Callback
):
Then when you want to retrieve this data (e.g. browsefile_Callback
):
...
myData = guidata(hObject);
myData.filename = uigetfile(...);
set(handles.showfile, 'String', filename);
和buttonwritecsv_Callback
:
myData = guidata(hObject);
new = imread(myData.filename);
...
基本上,guidata(hObject, myData)
允许您存储myData
,而myData = guidata(hObject, myData)
允许您检索它.
Basically, guidata(hObject, myData)
lets you store myData
and myData = guidata(hObject, myData)
lets you retrieve it.
这篇关于如何在MATLAB GUI中的函数之间传递值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!