通过MATLAB GUI使用不同的按钮功能导出UITable数据

时间:2014-08-27 04:31:39

标签: matlab matlab-figure matlab-uitable

我正在从数据库中执行简单的数据提取并将内容导出到Excel电子表格。我有两个按钮:

  1. 从数据库中提取数据并使用uitable显示它们。
  2. uitable数据导出到Excel电子表格。
  3. 问题:

    我似乎无法将这两个功能联系在一起。它不断抛出以下错误:

    堆栈跟踪:

    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
    

    示例图片:

    enter image description here

    代码 - 提取数据

    % --- 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];
    

    代码 - 导出UITable数据

    % --- 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
    

    基本上,我在num - >中引用了变量function pushbutton3_Callbackxlswrite('test.xls',num),取自function generateWAR,用于编译列和数据元素。

    我希望得到一些帮助。

1 个答案:

答案 0 :(得分:0)

变量num不在pushbutton3_Callback的范围内。根据您的整体架构以及是以编程方式还是通过GUIDE编码GUI,您可以在pushbutton3_Callback内使此变量可用。

为了查看函数范围内的内容,只需将whos添加到函数的开头,就像这样

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