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!