How to run a main program by sub program?

9 views (last 30 days)
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
DhanaLakshmiR
DhanaLakshmiR on 1 Feb 2018
initially im starting my program with time=0,before the end of the FOR loop time will be incremented as time+0.01(sampling time). So upto 3000 runtime my program values are processed with 0.01 seconds
Jos (10584)
Jos (10584) on 1 Feb 2018
Perhaps it is time you provide some exemplary code ...

Sign in to comment.

Answers (1)

Walter Roberson
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
DhanaLakshmiR
DhanaLakshmiR on 5 Feb 2018
how to call a function inside my program.can u give me answers?
Walter Roberson
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!