How to run a main program by sub program?
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Verification Mode 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!