필터 지우기
필터 지우기

Two matlab programs in one m file.

조회 수: 4 (최근 30일)
Daniel
Daniel 2011년 8월 2일
댓글: Walter Roberson 2020년 9월 16일
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
PRITI Pallavi pattanaik 2020년 9월 16일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by