How to run a main program by sub program?
11 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 1 Feb 2018
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 Comments
Walter Roberson
on 5 Feb 2018
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.
See Also
Categories
Find more on Verification Mode 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!