How do I save data created inside a parfor inside an app?

I have an app which ises a parfor to do major calculation the result of which is a large matrix for each parfor loop that I wish to save.
essentially I have
function ContinueButtonPushed(app, event)
app.TabGroup.SelectedTab = app.SetUpTab;
%app.RUNButton.Enable = 0;
app.comment.Value = 'Running...';
% lots of set up stuff here
parfor loop = startloop:endloop
%************************************
%create a name for the save file
%************************************
fl = join([f num2str(loop) '.mat']);
...
U = zeros(M+2,NJl); %output file
...
for j=1:NJl %produces an array 1000x1000 and bigger
U1=AA\(1i.*U0l - AB*U0l - sp*dtl*(U0l.*conj(U0l)).*U0l - B(:,j)-B(:,j+1));
UC = (U1+U0l)/2;
U1=AA\(1i.*U0l - AB*U0l - sp*dtl*(UC.*conj(UC)).*UC...
- B(:,j)-B(:,j+1))- deltaNoise(j);
U0l=U1; U(:,j) = [Ul(j+1);U1;Ur(j+1)];
end
parsave(fl,U0l); %cannot use save within a parfor
end
%**********************************************************************
% create a name and then save the parameters for later (P_X) file
%**********************************************************************
g = join(['U_L' num2str(app.xmax) 'run' num2str(app.runnumber) 'P_X.mat']);
app.loopmax = app.loopmax+app.offset;
othersave(app,g);
delete(gcp('nocreate'));
end
If I use parsave like this I get an error that I need to use
parsave(app,fl,U0l);
However this gives an error because the app value cannot be used in the parfor loop. One suggestion was to use a static function
methods (Access = private, Static)
function parsave(fname, vr)
save(fname, 'vr', '-v7.3')
end
end
Still doesn't work. I really need to save this data. If it is not in the app then the save works fine...

6 Comments

Hello,
you can't save an app object, but you can get assign value from its properties to a variable and save it.
I'm not trying to save an app object. The value I want to save is <U> which is a large matrix created inside the parfor. It is a local variable. The problem is the parfor loop. You cannot save inside it so you have to call a function which is what parsave is for. However this seems to have a problem as well. If you correctly define parsave(app,fname vr) then it fails because you cannot use an app designation inside the parfor and then if you go for a static parsave which does not require the app information then it is not recognised and I get an error also!
I suspect that the answer is related to this question
but I cannot actually figure out how.
If this were not an app then it is not a problem somehow. My code here works a treat.!!
parfor loop = (1+offset):(loopmax+offset)
%************************************
%create a name for the save file
%************************************
f= join(['U_L' num2str(xmax) 'run' num2str(runNo) 'P_' num2str(loop) '.mat']);
.... %lots of code
f %quick indicator that a loop has finished
parsave(f,U); %cannot use save within a parfor
end
%**********************************************************************
% create a name and then save the parameters for later (P_X) file
%**********************************************************************
... %more code to finish
function parsave(file, vr)
save(file, 'vr', '-v7.3')
end
In general, you can't save app. Let's move on from there.
othersave(app,g); % I don't know what this is
In the post, the code was set up as a function, so that after it ends, it outputs the result. You can do the same. In the callback call the function and save the result.
My thanks for your thoughts. I have realised the problem is made worse because my explantaion is not well thought through!
My parfor generates a large local matrix called <U>. I need to save it. I cannot do this inside the parfor so I call a function parsave. This will not work in the app as it did in the script. I have tried making parsave a static function but then the parfor does not see it! I cannot hang them all on another variable as say X(parforLoop#) = U and then return them as there may be 100+ of U and we get an out of memory. Basically I need for parsave to work somehow!?
External function in its own .m file instead of static class function.
I think you could be right. Rather defeats the object in terms of packaging but I haven't come up with anmything better. I will give it a go and see what turns up! Tx

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 12 Feb 2021

Commented:

on 13 Feb 2021

Community Treasure Hunt

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

Start Hunting!