简体   繁体   English

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

[英]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 假设我有一些函数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

I then need these variables passed into another function MS_Banding_Streaking() 然后我需要将这些变量传递给另一个函数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 文件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 文件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 只需在同一个.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量从一个函数传递到另一个函数? - How do I pass a variable from one function to another function? JavaScript:使用setInterval()将变量从一个函数传递到另一个函数? - JavaScript: Pass a variable from one function to another, using setInterval()? 如何在一个页面中将变量从一个函数传递给另一个函数? - how to pass variable from one function to another in the same page in php? 如何在Perl中将变量从一个函数传递到另一个函数 - How do I pass in a variable from one function into another in perl 如何将字符串变量从一个 function 传递给另一个? - How to pass a string variable from one function to another? 如何将变量从一个 function 传递到另一个 Javascript? - How to pass variable from one function to another Javascript? 如何将PHP变量从一个函数传递给另一个函数? - How to pass a PHP variable from one function to another? 如何将 PHP 变量从一个函数传递到另一个函数? - How do I pass a PHP variable from one function to another? 如何在不使用全局变量的情况下将列表从一个 function 传递到另一个? - How to pass list from one function to another without using global variable? 将变量从一个函数传递给另一个函数 - Pass a variable from one function to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM