0

我正在尝试将图像文件转换为二进制文件,我在 Matlab GUI 中使用了 2 个按钮。第一个按钮用于浏览文件,然后在静态文本上显示文件名,第二个按钮用于将其保存为 CSV 文件,这是我的代码

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”让“另存为”按钮能够从“浏览”按钮获取变量,但是当我在浏览文件后单击“另存为”按钮时,它什么也不做......

我是否遗漏了某些东西或在使用它时犯了错误?

在我遵循@Richante 的答案后,我得到了以下错误,我不明白

??? 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{:});

我不知道是什么。。

4

1 回答 1

1

是的,这不太对。不要handles用于存储数据 - 使用您自己的结构。在OpeningFcn

myData.filename = [];
guidata(hObject, myData);

然后,当您要检索此数据时(例如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)让您存储myDatamyData = guidata(hObject, myData)让您检索它。

于 2012-04-12T18:27:49.187 回答