# How to save output of 'fit' optimization routine in PARFOR loop

3 views (last 30 days)
roni zidane on 7 Jan 2019
Answered: rakbar on 7 Jan 2019
Hi There,
Since in my last question it was mentioned that i can't use Multistart for 'Fit', i would have to run the Multi combination process manually. Hence, the following....
I have
• single pixel data in X and Y
• My model is 'fparam'
• Lower bound 'lb'
• Upper bound 'ub'
• custom starting points in 'custmPt' here attached as startpoints.mat
• size of the custmPt is (584497 x 4) means i have 584497 different combinations that i need to run 'fit' process on.
Since, i need to repeat the whole combination process for each pixel of my mask as shown in fig.1, running fitting process for each combination of startingpoint will be very time consuming, i want to use 'parfor' and run the fitting procedure.
However, as per the MATLAB documentation Fit in Matlab , 'fit' generates 'struct' as output, i am not sure how i can SAVE them while running PARFOR. This structures will have several fields which i will use them later to select best solution of the process.
So, in terms of algorithm, i want to do the following...
-> for each pixel of the mask
create custom startpoints within bound
parfor (try different startpoint combinations and FIT process) % parallel process
save output of FIT process (structures) from each combination
end
select best solution
repeat the for loop.
end
fparam = fittype(@(a,b,c,d,x)(a)*exp(-(1/b)*x)+abs(1-a)*(exp(-(1/c)*x))+d);
lb = [0,0,0,0];
ub = [1,30,200,80];
x = [3.39,8.59,13.8,19,24.2,29.4,34.6,39.81,45.0,50.21];
y = [1,0.2905,0.0894,0.0838,0.1173,0.1006,0.0782,0.0894,0.1061,0.0726]
parfor Yt = 1:size(custmPt,1)
aL2 = custmPt(Yt);
opts = fitoptions('Display','Off','Method','NonlinearLeastSquares','Normalize','Off',...
'Startpoint',aL2, 'Robust', 'On', 'Lower',lb,'Upper',ub,...
'TolFun',1e-3);
[estTmp,Goft,Out] = fit(x,y,fparam,opts);
% /\ /\ /\
% || || ||
%
%
% /\
% || This arrows show that they are structures containing output of the fit process
%%%%%%%%%%%%%%%%%%%%%%% Still need to implement the saving of parameters to use them outside PARFOR loop %%%%%%%%%%%%%%%%%%%%%%%%%%%
end
So, how can i implement saving of 'Struct' in 'PARFOR' so i can access them later for further processing ?
Thank you for your help

rakbar on 7 Jan 2019
You need to create a new function, e.g., parsave, and pass in your struct. Within this new function, you can then save your struct, either in CSV, or any other format that you like. For example:
parfor Yt = 1:size(custmPt,1)
aL2 = custmPt(Yt);
opts = fitoptions('Display','Off','Method','NonlinearLeastSquares','Normalize','Off',...
'Startpoint',aL2, 'Robust', 'On', 'Lower',lb,'Upper',ub,...
'TolFun',1e-3);
[estTmp,Goft,Out] = fit(x,y,fparam,opts);
%%%%%%%%%%%%%%%%%%%%%
% New parsave function
parsave(estTmp,'filed_name')
%%%%%%%%%%%%%%%%%%%%%
end
% make a new function to save your sruct
% modify internals based on how you want to save the data
function parsave(data_save,file_name)
save(file_name,data_save) % this will save a *.mat file at each loop iteartion.
end

### Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

R2016b

### Community Treasure Hunt

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

Start Hunting!

Translated by