Question about calling M-files in a for loop

2 views (last 30 days)
Valerie
Valerie on 17 Mar 2015
Commented: dpb on 17 Mar 2015
I am trying to write an m-file that loads up a large data set then loops through a number of time segments and selects an appropriate plotting script to plot each segment. It's organized something like this:
% Load large data set with lots of variables
load('my_large_data_set')
for i = 1:num_segments
% Set start and end times to plot
start_time = start_times(i);
end_time = end_times(i);
% Set which plot script to use (In the real file this logic looks at the data within the time segment and determines from the data which plotting script is most appropriate. For simplicity, I just have it selecting from a vector of pre-determined values here.)
plot_script_number = plot_script_types(i);
% Plot the data
switch plot_script_number
case(1)
My_Plot_Script_1
case(2)
My_Plot_Script_2
case(3)
My_Plot_Script_3
end
end
My problem is that the for loop does not wait for the plotting script to finish running before going on to the next value of i. The current version I'm using will run the first My_Plot_Script, but by the time it has finished, the for loop is done running so it doesn't ever call another plotting script.
The script needs to run automatically, so "pause" doesn't seem to be an option. I also don't think I want to turn the plotting scripts into functions because then I'd either have to pass all of the data into the function (I think) or reload the data inside the function on each iteration.
Is there some other solution I'm not seeing?
Thanks,
Valerie
  1 Comment
dpb
dpb on 17 Mar 2015
AFAIK Matlab is still single-threaded...ergo, functions should run to completion in sequence. Have you done something inside these plotting script m-files to spawn a new process or use the parallel-computing toolbox? That seems to me to be the only way this can happen unless the symptom is being camouflaged and being mistaken for something entirely different.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!