I want to read a large amount of files, process each of them and save the results for each of them in a .mat file. The processing of each file is independent from the others, so I'd like to try using parfor. I have written the following Matlab script file:
load filelist
obj = package.name.SomeObject();
matlabpool local 5
parfor i=1:length(filelist)
result = obj.compute(filelist{i});
[~, name, ~] = fileparts(filelist{i});
save(['~/path/' name], 'result');
end % file loop
matlabpool close
When I try to run it on my computer, a Matlab pool is intialized (connected to 5 workers), but then the following error message occurs:
Error using parallel_function (line 589)
Undefined function or variable "cleaner".
Error in readfiles (line 14)
parfor i=1:length(filelist)
Error in run (line 64)
evalin('caller', [script ';']);
Do you know where the problem could be?
I don't know why (there might be a connection to this issue), but the problem was solved by enclosing the code inside a function and calling that function (instead of calling the script file by run script.m). It also required creating a parsave function (see explanation here).
Related
The code is as follows:
k=input('enter k');
i=0:1:k;
lambda=4;
cdf = exp(-lambda) .* ((lambda.^i)./ factorial(i));
plot(i,cdf);
When running this code, I am getting the following error:
Error using plot
Attempt to execute SCRIPT newplot as a function:
C:\Users\Sudhanshu ranjan\MATLAB\R2016a\toolbox\matlab\graphics\newplot.m
Error in Untitled (line 9)
plot(i,cdf);
My mynewplot.m file is as follows:
p = [0:0.00001:1]
a =p.* log(2.*(p))+(1-p).* log(2.*(1-p));
plot(p,a)
How do I resolve this error?
There's an inbuilt MATLAB function named newplot, which seems to be called when running a plot command. By defining a custom script named newplot.m, you're shadowing the functionality of MATLAB's newplot, thus the plot command tries to execute a FUNCTION newplot, but only finds your SCRIPT newplot.
You can resolve that issue by simply renaming your script, e.g. mynewplot.m.
I get this error in Matlab
Attempt to execute SCRIPT feature as a function:
E:\feature.m
Error in images.internal.isFigureAvailable (line 9)
if feature('showFigureWindows')
Error in imtool (line 172)
if ~images.internal.isFigureAvailable()
Error in Untitled (line 8)
imtool close all; % Close all imtool figures.
Error in run (line 91)
evalin('caller', strcat(script, ';'));
It does not matter which code I run it always shows up. How can I solve it?
MATLAB has a built in function called feature. imtool is internally trying to call this function, but instead a script you placed on E:\feature.m is called. Delete or rename your feature.m.
The following code shows this error
Error: The variable out in a parfor cannot be classified.
N=5;
ini = 3;
l=ini:(N+ini-1);
out=cell(1,N);
parfor i=l
out{i+(1-ini)} = func(i);
end
The problem occurs when a variable like ini is an index of any variable inside a parfor loop.
Can ini be introduced in such way that the code works without changing the structure?
MATLAB version R2017a.
I try to run a model in a loop using parfor command.
So I wrote the following code:
% control_model.m
warning('off', 'Simulink:Engine:UsingDefaultMaxStepSize');
apool = gcp('nocreate');
if isempty(apool)
apool = parpool('local');
end
load_system('mymodel');
tic
parfor w=1:10
warning('off', 'Simulink:Engine:UsingDefaultMaxStepSize');
w_str=num2str(w);
set_param('mymodel/mysystem','sys', ['tf(',w_str,',[1 ',w_str,'])'] )
sim('mymodel',[],[])
drawnow
end
toc
close_system('mymodel',0);
% delete(poolobj)
Then I will get the following error:
Error using control_model (line 11)
Invalid Simulink object name: mymodel/mysystem
If I run it again, I get the same error (Although using for instead of parfor solves this error).
But if I put a load_system('mymodel'); just after parfor and run it once, the problem is fixed. And even if I remove load_system command, the error is not shown for the next times anymore.
I am interested in knowing what is happening behind the scene and why the first load_system does not solve the problem even if I run the program so many times. while the one in parfor fix the problem even if being removed in the next time calling the script?
I think you need to load the model on the worker (see sim in parfor with Normal Mode in the documentation for more details), so your load_system needs to be within the parfor loop:
tic
parfor w=1:10
load_system('mymodel');
warning('off', 'Simulink:Engine:UsingDefaultMaxStepSize');
w_str=num2str(w);
set_param('mymodel/mysystem','sys', ['tf(',w_str,',[1 ',w_str,'])'] )
sim('mymodel',[],[])
drawnow
end
toc
I am trying the matlab parallel processing and during this I tried to apply this thing to my code. Below is the code.
matlabpool open 2
pop = create_population(match_matrix,PopSize);
ftns = zeros(PopSize,1);
parfor i=1:PopSize
ftns(i) = get_fitness(pop{i});
end
matlabpool close
The error I am getting is following
Error using parallel_function (line 589)
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error stack:
SWIFTga>(parfor body) at 127
Error in SWIFTga (line 126)
parfor i=1:PopSize
I am getting no errors if I replace the parfor with for. Please suggest what may be going wrong....
I tried so much and at last found the problem. Actually I was accessing global variables in the function called inside parfor body. From the matlab documentation found that we cant access global variables in parfor as there is synchronization issues. I has to change the code from global variables to argument passing. Thanks all for those who put some time into my question.