如何使用MatLab将变量从一个函数传递到另一个函数?

时间:2012-08-02 18:17:00

标签: function matlab variables

假设我有一些函数import_data(),在这个函数内部我创建了2个变量:response_valuesfile_to_get

file_to_get = uigetfile({'*.csv*'}, 'Select the CSV File',...
'\\pfile01thn\bbruffey$\My Documents\analysis data\full files\...
Raw Stats Data full file only');

response_values = zeros(numel(C),numCols);
for i=1:numel(C)
    v = textscan(C{i}, '%s', 'Delimiter',',');
    v = str2double(v{1}(4:end));
    response_values(i,1:numel(v)) = v;
end

然后我需要将这些变量传递到另一个函数MS_Banding_Streaking()

怎么可以这样做? (我现在正在使用全局变量,这是非常糟糕的做法。

2 个答案:

答案 0 :(得分:2)

这样的东西

file import_data.m

function response_values, file_to_get = import_data()

file_to_get = uigetfile({'*.csv*'}, 'Select the CSV File',...
'\\pfile01thn\bbruffey$\My Documents\analysis data\full files\...
Raw  Stats Data full file only');

response_values = zeros(numel(C),numCols);
for i=1:numel(C)
    v = textscan(C{i}, '%s', 'Delimiter',',');
    v = str2double(v{1}(4:end));
    response_values(i,1:numel(v)) = v;
end

文件mainfunc.m

% Stuff before
[vals, filegot] = import_data()
MS_Banding_Streaking(filegot, vals)
% Stuff after

答案 1 :(得分:1)

只需在同一.m文件中编写两个函数

即可
function import_data()    
file_to_get = uigetfile({'*.csv*'}, 'Select the CSV File',...
'\\pfile01thn\bbruffey$\My Documents\analysis data\full files\...
Raw  Stats Data full file only');

response_values = zeros(numel(C),numCols);
for i=1:numel(C)
    v = textscan(C{i}, '%s', 'Delimiter',',');
    v = str2double(v{1}(4:end));
    response_values(i,1:numel(v)) = v;
end

MS_Banding_Streaking(response_values, file_to_get);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function MS_Banding_Streaking(resp_value, f2g)
% function body