-1

I am performing a simple data extraction from a database and exporting the contents to a Excel Spreadsheet. I have two buttons:

  1. To extract data from the database and display them using uitable.
  2. 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:

enter image description here

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.

Community
  • 1
  • 1
Jeiman
  • 1,121
  • 9
  • 27
  • 50

1 Answers1

0

Variable num is not in the scope of pushbutton3_Callback. You have various possibilities to make this variable available within pushbutton3_Callback depending on your overall architecture and depending on whether you code your GUI programmatically or via GUIDE.

In order to see what is in your function scope simple add whos to the beginning of your function, like so

function pushbutton3_Callback(hObject, eventdata, handles)    
whos
button = questdlg('Would you like to Export the data to a ... 
    folder?','Confirm','Yes','No','Cancel Program','No')
% and so on
georg
  • 635
  • 7
  • 16
  • I am using GUIDE to build the GUI. Furthermore, using `whos` returns the variables for `pushbutton3_Callback`, not the variables stored in the workspace. – Jeiman Aug 27 '14 at 12:29
  • Yep, that's the intention. You get an error telling you that `num` is not in the current scope. So to learn what your scope is and in which scope your callback lives (it might be that your callback is nested into some other functions) you can use whos (or set breakpoint as an alternative). As a initial work-around you might simply make `num` globally available. – georg Aug 27 '14 at 13:33