简体   繁体   中英

How to pass a variable from one function into another using MatLab?

Let's say I have some function import_data() and inside of this function I am creating 2 variables: response_values and file_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

I then need these variables passed into another function MS_Banding_Streaking()

How could this be done? (I'm using globals at the moment which is extremely bad practice.

Something like

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

file mainfunc.m

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

Just simply write the two functions in the same .m file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM