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/variables/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
Matlab:pass';世卫组织';作为参数输出_Matlab_Variables - Fatal编程技术网

Matlab:pass';世卫组织';作为参数输出

Matlab:pass';世卫组织';作为参数输出,matlab,variables,Matlab,Variables,我编写了一个函数,它接受输入变量的名称和值,并将它们写入一个文件。例如 a = 10; b = 100; writevars('file.txt',a,b); 给我一个文件file.txt,其中包含: \def\a{\num{10}} \def\b{\num{100}} 它现在希望能够传递使用who命令找到的所有变量。例如,如果who返回: a b z 我希望能够像调用writers('file.txt',a,b,z)一样使用writevars 我遇到的主要问题是writeva

我编写了一个函数,它接受输入变量的名称和值,并将它们写入一个文件。例如

a = 10;
b = 100;
writevars('file.txt',a,b);
给我一个文件
file.txt
,其中包含:

\def\a{\num{10}}
\def\b{\num{100}}
它现在希望能够传递使用
who
命令找到的所有变量。例如,如果
who
返回:

a    b    z
我希望能够像调用
writers('file.txt',a,b,z)
一样使用
writevars

我遇到的主要问题是writevars使用了
inputname
。。。(临时变量不起作用,例如,
writevars('file.txt',100)
不起作用,因为文件中没有指定名称)

答复
它可以通过使用whos命令的返回值来使用:

function GetAllVars
a = 45;
x = 67;    
ff = 156;
z = who();
for i=1:numel(z)
    if ~isequal(z{i},'z')
        fprintf(1,'%s = %f\n',z{i},eval(z{i}));
    end
end
您可以使用从
writevars
中运行
who
,例如

function writevars(filename,varargin)

%# get a list of variable names in the calling workspace and read their values
if isempty(varargin)
   listOfVars = evalin('caller','who');
   values = cell(size(listOfVars));
   for i=1:length(listOfVars)
      values{i} = evalin('caller',listOfVars{i});
   end
else
   %# use inputname to read the variable names into listOfVars
end

%# --- rest of writevars is here ---

如果在基本工作区中定义了变量,这将不起作用。没有从WriteArs内部使用它,但是
evalin
被证明是有用的。有关说明,请参见我的编辑。
function writevars(filename,varargin)

%# get a list of variable names in the calling workspace and read their values
if isempty(varargin)
   listOfVars = evalin('caller','who');
   values = cell(size(listOfVars));
   for i=1:length(listOfVars)
      values{i} = evalin('caller',listOfVars{i});
   end
else
   %# use inputname to read the variable names into listOfVars
end

%# --- rest of writevars is here ---