Two matlab programs in one m file.
2 views (last 30 days)
Show older comments
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 Comments
PRITI Pallavi pattanaik
on 16 Sep 2020
Enabling Device to Device communication in millimeter wave 5G
Walter Roberson
on 16 Sep 2020
PRITI Pallavi pattanaik: it is not at all clear how your comment relates to the Question that Daniel asked?
Accepted Answer
Oleg Komarov
on 2 Aug 2011
function out = foo(in)
% Do stuff
out = subfoo(inmod)
end
function processed = subfoo(inmod)
% Do stuff
end
3 Comments
Oleg Komarov
on 2 Aug 2011
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
on 3 Aug 2011
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*.
More Answers (2)
Norman Johnson
on 2 Aug 2011
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 Comment
Oleg Komarov
on 2 Aug 2011
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.
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!