How to run a main program by sub program?
이전 댓글 표시
I am having a main program that contains for loop that will be executed for more than 10,000 samples and it run for 0.01 seconds.If i include sub program as sub.m in main program that sub program has also for loop for 3000 samples.so,for each 0.01 seconds of main program the sub program will run upto 3000 samples.can the sub program will run one time and give results? If you can't get my question kindly apologize me and ask i will explain clearly. Kindly give answers.
댓글 수: 7
Walter Roberson
2018년 2월 1일
I think I understand everything until the "can the sub program will run one time and give results?" part, but I do not understand what you are asking there?
Jos (10584)
2018년 2월 1일
I am a little confused ... Can't you just edit the loop to run only once?
for k=1:1
...
You can also set a breakpoint at the appropriate place to halt execution at the end of the loop.
Jan
2018년 2월 1일
I do not understand the question. Of course the sub program can run and reply its outputs. But what is the meaning of the 0.01 seconds? Is this a simulated time or runtime?
DhanaLakshmiR
2018년 2월 1일
DhanaLakshmiR
2018년 2월 1일
DhanaLakshmiR
2018년 2월 1일
Jos (10584)
2018년 2월 1일
Perhaps it is time you provide some exemplary code ...
답변 (1개)
Walter Roberson
2018년 2월 1일
No. If you want the subprogram to run only once, then you need to change the code so that it runs only once. You could consider doing that with some kind of test of conditions about how often it should run.
For example,
if exist('runOnce', 'var')
last_iteration = 1;
else
last_iteration = 3000;
end
for loop_counter = 1 : last_iteration
...
end
Then if you wanted the subprogram to only execute once, you would assign a value to the variable runOnce -- any value.
... This is, however, not what I would recommend doing. I would instead suggest you create a function for your subprogram, and that your function should accept a parameter that indicates how often to run.
댓글 수: 5
DhanaLakshmiR
2018년 2월 1일
Guillaume
2018년 2월 1일
Even better, would be to extract the content of the inside of the loop of the sub-program into its own function. It is then the job of whichever function is calling that new function to decide how many time to call it.
DhanaLakshmiR
2018년 2월 1일
DhanaLakshmiR
2018년 2월 5일
Walter Roberson
2018년 2월 5일
The only way to call a function inside a .m file that is not the main function for the .m file, is if you have somehow obtained a function handle for it. If you do have a function handle somehow then call it like any other function handle.
But very likely you should instead be breaking up your functionality, like
function do_100_steps(.....)
for step = 1 : 100
do_1_step(.....);
end
and do all the real work in do_1_step which would have its own .m file and could be called directly by the program that only wants to process one step at a time.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!