I am performing a simple data extraction from a database and exporting the contents to a Excel Spreadsheet. I have two buttons:
- To extract data from the database and display them using
uitable
. - To export the
uitable
data to Excel spreadsheet.
Problem:
I can't seem to link the two functions together. It keeps throwing the following error:
Stacktrace:
Undefined function or variable 'num'.
Error in FatherSonGUI>pushbutton3_Callback (line 155)
xlswrite('test.xls',num)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in FatherSonGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)FatherSonGUI('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Sample Picture:
Code - Extract Data
% --- Executes on button press in GenerateData.
function generateWAR(hObject, eventdata, handles)
f = gcbf(); % size of the figure object
dat = dyn_conformer.Data;
set(f,'name','Father & Son War Room','numbertitle','off') %renames the Title Figure
cnames = {'PROCESS STATUS ID','TASK ID','TASK TYPE', 'CARTRIDGE ID', 'DISPLAY ORDER'};
rnames = {'1','2','3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20','21', '22', '23', '24', '25'};
t = uitable('Parent',f,'Data',dat,'ColumnName',cnames,...
'RowName',rnames,'Position',[10 100 1150 370]); % size of the values inside the figure object
col = get(t,'ColumnName');
data = get(t,'Data');
%num = [col';data(:,1:length(col))];
num = [col';data];
Code - Export UITable Data
% --- Executes on button press in ExportData.
function pushbutton3_Callback(hObject, eventdata, handles)
button = questdlg('Would you like to Export the data to a folder?',...
'Confirm','Yes','No','Cancel Program','No')
if strcmp(button,'Yes')
FileName = uiputfile('*.xls','Save as');
xlswrite('test.xls',num)
elseif strcmp(button, 'No')
helpdlg('File not exported!','Message')
elseif strcmp(button, 'Cancel Program')
helpdlg('Program cancelled!','Message')
end
Basically, I have referenced variable num
in function pushbutton3_Callback
--> xlswrite('test.xls',num)
, which is taken from function generateWAR
where it compiles the column and data elements.
I would appreciate some help on this.