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

通过MATLAB GUI使用不同的按钮功能导出UITable数据,matlab,matlab-figure,matlab-uitable,Matlab,Matlab Figure,Matlab Uitable,我正在从数据库执行简单的数据提取,并将内容导出到Excel电子表格。我有两个按钮: 从数据库中提取数据并使用uitable显示它们 将uitable数据导出到Excel电子表格 问题: Undefined function or variable 'num'. Error in FatherSonGUI>pushbutton3_Callback (line 155) xlswrite('test.xls',num) Error in gui_mainfcn (line 95)

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

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

    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
    
    我似乎无法将这两个函数连接在一起。它不断抛出以下错误:

    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
    
    示例图片:

    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
    

    代码-提取数据

    % --- 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];
    
    % --- 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
    
    代码-导出可编辑数据

    % --- 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];
    
    % --- 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
    
    基本上,我在
    函数按钮3\u Callback
    -->
    xlswrite('test.xls',num)
    中引用了变量
    num
    ,该变量取自编译列和数据元素的
    函数生成器WAR


    如果您能在这方面提供帮助,我将不胜感激。

    变量
    num
    不在
    按钮3\u回调的范围内。您可以在
    pushbutton3\u Callback
    中使用此变量,具体取决于您的总体架构,以及您是以编程方式还是通过指南编写GUI代码

    为了查看函数范围中的内容,只需在函数开头添加
    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
    

    我正在使用GUIDE构建GUI。此外,使用
    whos
    返回
    pushbutton3\u Callback
    的变量,而不是存储在工作区中的变量。是的,这就是目的。您会收到一个错误,告诉您
    num
    不在当前范围内。所以,要了解您的作用域是什么以及回调位于哪个作用域中(可能是回调嵌套在其他一些函数中),您可以使用whos(或者设置断点作为替代)。作为一项初步工作,您可以简单地使
    num
    全局可用。