MATLAB: Matlab Parfor function. how to use it with the current situation

MATLABparallel computingParallel Computing Toolboxparfor

Dear engineer friends in this community,
I'm having a issue with the parfor.
Actually, I have a pre-defined the function, which was not built with the idea of 'parfor'. So it is very hard (for me) to alter it to fit the 'parfor' principle. Let's note it as 'f()'.
But the good part is, this function f() need to be run for many times to acquire enough results of data. So previously, I open several matlab in my computer to run it 'parallel' to speed up the running time.
Like below,
%matlab 1
for iteration 1:5000
f();
end
%matlab 2
for iteration 5001:10000
f()
end
...
But it is not a wise way, since I know we have the 'parfor' function to automatically do this work for me. Then I look up the tutorial, the 'parfor' could divide the iterations to several parts, as long as each iteration are independent. (if my understanding is correct)
So I tried this:
parfor 1: 1000000
f();
end
But the matlab gives me error says the it violates the classifications of the variables for 'parfor'…
I am wondering, if anyone could enlight me, when we use 'parfor', is it that I have to make sure every inner for-loop is also self-independent? Why is that???
I can manually do 'parallel', why can't 'parfor' do it??

Best Answer

Haha, I found the answer myself... And I shared it here.
I think the issue is one of matlab or me is not that smart. So directly add a parfor outside the for-loop will not bypass the classification of variable error.
% this cannot work, because f() has variables that violating parfor function...
parfor ...
[
f()
...
]
end
But, if I pacakge this f() function, and then use parfor to call this f(). It works.
% package the f(), and then call the f() inside the parfor.
% this works.
f()
[
...
]
parfor ...
f();
end
Good day folks.