0

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);

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?

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

I dont know what is it..

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45

1 Answers1

1

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);

Then when you want to retrieve this data (e.g. browsefile_Callback):

...
myData = guidata(hObject);
myData.filename = uigetfile(...);
set(handles.showfile, 'String', filename);

and buttonwritecsv_Callback:

myData = guidata(hObject);
new = imread(myData.filename);
...

Basically, guidata(hObject, myData) lets you store myData and myData = guidata(hObject, myData) lets you retrieve it.

Richante
  • 4,353
  • 19
  • 23
  • okay @Richante , thanks for your advise, but I still get some errors, I will post it –  Apr 12 '12 at 19:28
  • 6
    I do think this answer is correct. I believe you do want to use the handles structure. MATLAB documentation states, __"GUIDE uses guidata to store and maintain the handles structure. In a GUIDE GUI code file, do not overwrite the handles structure or your GUI will no longer work. If you need to store data other than handles for your GUI, you can add new fields to the handles structure and safely place your data there."__ – wherestheforce Sep 21 '12 at 15:05