Two matlab programs in one m file.

2 次查看(过去 30 天)
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
Enabling Device to Device communication in millimeter wave 5G
Walter Roberson
Walter Roberson 2020-9-16
PRITI Pallavi pattanaik: it is not at all clear how your comment relates to the Question that Daniel asked?

请先登录,再进行评论。

采纳的回答

Oleg Komarov
Oleg Komarov 2011-8-2
Use a subfunction but be the subfunction cannot have the same name ans the main function:
function out = foo(in)
% Do stuff
out = subfoo(inmod)
end
function processed = subfoo(inmod)
% Do stuff
end
  3 个评论
Oleg Komarov
Oleg Komarov 2011-8-2
I thought about that, but the OP is not clear enough and besides if the level of recursion is known (since he's setting up a loop) I would still choose a subfunction with no checks.
Walter Roberson
Walter Roberson 2011-8-3
I don't think this possibility answers the question *as asked*.
The question as asked implies that the first function does something useful that the user might want to call from some other routine. One of the things the user would like to be able to do with that first function is call it in a loop with various inputs, and the user would wondering about combining those two in to the same file. The answers (other than the one I gave) result in the first function not being accessible outside of the file. In order to have access to _both_ behaviors in the same file, of doing the useful work or of (optionally) looping over a set of values, you need the switchyard approach or you need to code the parameters in such a way that the two cases can be differentiated.
Of course it is possible that the result that was _really_ desired was what the other people happened to answer, but that wasn't (in my opinion) what was *asked for*.

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2011-8-2
You would need a "switchyard" approach. See for example here

Norman Johnson
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
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by