Two matlab programs in one m file.
显示 更早的评论
I wrote a two functions in matlab. The first takes 2 input parameters, and does its thing. The second is basically just a loop of the first program, where it loops the first function 4 times, to automate a task. The first function takes data from 1 of 4 databases, and the looping function has those names in a list so it does all 4 in an automated fashion.
function output=Populate(DB_name)
names={'1stname','2ndname', '3rdname', '4thname'};
for i=1:4
Populate(DB_name,names{i})
end
I was wondering if there is any way to put both programs together in one. Right now, I have 2 .m files, which is more to worry about making sure all the users have, especially when one is just an intermediary. I would like to know if I could call a loop of a program within the same file the program is in.
2 个评论
PRITI Pallavi pattanaik
2020-9-16
Enabling Device to Device communication in millimeter wave 5G
Walter Roberson
2020-9-16
PRITI Pallavi pattanaik: it is not at all clear how your comment relates to the Question that Daniel asked?
采纳的回答
更多回答(2 个)
Walter Roberson
2011-8-2
0 个投票
Norman Johnson
2011-8-2
It's pretty simple. Below is a generic example.
function [out1a, out2a, out3a] = func1(in1a, in2a, in3a)
% calculations
[out1b, out2b, out3b] = func1(in1b, in2b, in3b)
% more calculations
end
function [out1b, out2b, out3b] = func1(in1b, in2b, in3b)
% even more calculations
end
This can all be written in a single m-file as long as each function is terminated with an 'end'. The fist function goes first (i.e. the caller).
If I understand your code correctly, the m-file should read as follows:
function names = CallerFunc
for i=1:4
Populate(DB_name,names{i})
end
end
function output=Populate(DB_name)
names={'1stname','2ndname', '3rdname', '4thname'};
end
1 个评论
Oleg Komarov
2011-8-2
Yes, but you could also define nested functions, i.e. the a function within a function (the last end is for the caller/main function). The advantage is that the nested can "see" the variables of the main but not the other way around.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!