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 comentarios

Walter Roberson
Walter Roberson el 1 de Feb. de 2018
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?
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
Jan el 1 de Feb. de 2018
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
DhanaLakshmiR el 1 de Feb. de 2018
For each and every 0.01 seconds of main program the sub program will execute the entire for loop thats why im asking can the subprogram will run only once
DhanaLakshmiR
DhanaLakshmiR el 1 de Feb. de 2018
NO jos,i want to run that for 3000 samples then only i will get the required output
DhanaLakshmiR
DhanaLakshmiR el 1 de Feb. de 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) el 1 de Feb. de 2018
Perhaps it is time you provide some exemplary code ...

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 1 de Feb. de 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 comentarios

DhanaLakshmiR
DhanaLakshmiR el 1 de Feb. de 2018
is using function will resolve my problem.can you explain how to use those function
Guillaume
Guillaume el 1 de Feb. de 2018
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
DhanaLakshmiR el 1 de Feb. de 2018
Actually i have written code for the aircraft model includes that an aircraft avoiding obstacles. Now im trying for moving obstacle avoidance. so that i thought to use the code of an aircraft model as another moving obstacle and want to include in the whole model.
DhanaLakshmiR
DhanaLakshmiR el 5 de Feb. de 2018
how to call a function inside my program.can u give me answers?
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Feb. de 2018

Comentada:

el 5 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by