Clear Filters
Clear Filters

Is it possible to run multiple lines at once in a Matlab script?

7 views (last 30 days)
I am working on a project where I need to pull data from an external program called XFOIL. I have a function created called XFOIL which can run the program and pull data from it automatically. Unfortunately XFOIL takes time to run, and I have a lot of variation in the parameters of the data I need to collect. That means I need to run XFOIL a significant number of times. Currently my script runs the XFOIL functions sequentially which takes a while. Is there a way where I can run multiple functions at once in the same script? If I had the ability (I do have the computing power) to run 10 or so of these at the same time, it would significantly speed up the waiting time for data.
Example of what I mean, I would like to run these all at the same time instead of one after the other:
[pol1 foil1] = xfoil(myfoil1,alpha,Re_c,Mach,'ppar T 0.2','ppar n 330','oper iter 200');
[pol2 foil2] = xfoil(myfoil2,alpha,Re_c,Mach,'ppar T 0.2','ppar n 330','oper iter 200');
[pol3 foil3] = xfoil(myfoil3,alpha,Re_c,Mach,'ppar T 0.2','ppar n 330','oper iter 200');

Accepted Answer

Edric Ellis
Edric Ellis on 30 Nov 2023
To expand just a little on @Stephen23's comment - you can do this if you have Parallel Computing Toolbox. You should first restructure your code a little bit to make it amenable to running in a for loop, like this:
myfoils = {myfoil1, myfoil2, myfoil3, . . .};
for i = 1:numel(myfoils)
[pol{i}, foil{i}] = xfoil(myfoils{i}, . . .);
From there, you can replace for with parfor to have the iterations run in parallel. (I've written things as if you need cell arrays, but perhaps if the values are scalars, you could just use ordinary arrays)
One thing to be careful about - sometimes external programs use the filesystem for temporary working files etc. - if that is the case, you'll need to take a bit more care.
Timothy McEvoy
Timothy McEvoy on 30 Nov 2023
In the case where a program does use the file system for temporary working files, how would I go about making that work without running into issues?
Walter Roberson
Walter Roberson on 30 Nov 2023
  • Some external software uses hard-coded absolute-path temporary file names, and there just isn't much you can do
  • Some external software uses hard-coded temporary file names relative to the directory it is run in; in such a case you end up having to create one directory for each simultaneous execution in order to avoid having the files conflict with each other
  • Some external software permits the name of the temporary file to be passed to it
  • Some external software already uses randomized filenames and so just doesn't have this problem

Sign in to comment.

More Answers (0)


Find more on Programming 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!