Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将xlsread中的数据放入MATLAB中的全局变量中?_Matlab_Global Variables_Scope - Fatal编程技术网

如何将xlsread中的数据放入MATLAB中的全局变量中?

如何将xlsread中的数据放入MATLAB中的全局变量中?,matlab,global-variables,scope,Matlab,Global Variables,Scope,我使用uigetfile函数来选择XLS文件。然后,在我的GUI的按钮回调函数open_xls_callback中,我使用以下命令从excel文件中读取文本和数字值: [text,number,d] = xlsread(...) 如何访问其他calback函数(例如按钮)中的变量text和number?如何使这些变量成为全局变量并可从该功能范围之外访问?您可以使用assignin将变量放入给定的工作区。 在您的例子中,如果您指的是全局,那么您指的是基本工作区,然后使用assignin('bas

我使用
uigetfile
函数来选择XLS文件。然后,在我的GUI的按钮回调函数
open_xls_callback
中,我使用以下命令从excel文件中读取文本和数字值:

[text,number,d] = xlsread(...)

如何访问其他calback函数(例如按钮)中的变量
text
number
?如何使这些变量成为全局变量并可从该功能范围之外访问?

您可以使用
assignin
将变量放入给定的工作区。 在您的例子中,如果您指的是全局,那么您指的是基本工作区,然后使用
assignin('base','variable\u name\u you\u want',text)


如果确实需要全局变量,则在函数中声明
global var\u name\u\u want
,然后分配
var\u name\u\u want=text

所有嵌套函数都可以访问其父函数中定义的变量。这可用于在所有回调函数之间共享数据

下面是一个例子来说明:

function myGUI()
    %# this variable is accessible in both callback functions
    x = 0;

    %# a simple GUI to increment/show the variable x
    figure('Position',[300 300 350 150])
    uicontrol('style','pushbutton', 'String','increment', ...
        'Position',[50 50 100 30], 'Callback',@incrementCallback);
    uicontrol('Style','pushbutton', 'String','get', ...
        'Position',[200 50 100 30], 'Callback',@getCallback);

    %# callback functions
    function incrementCallback(src,evt)
        x = x + 1;
    end
    function getCallback(src,evt)
        msgbox( sprintf('x = %d',x) )
    end
end