How to save output(.mat) file for all generated values instead of last values ?

1 view (last 30 days)
I am trying to run the optimization using the following code. The code runs fine and evey thing is working.
But I wanted to saved all the values of x obtained in all iterations, but only the last value is saved in .mat file (screenshot attached).
I tried using the save after the function ends, but that results in error. Please help me...
-----------------------------------------------------------------------------------------------
function y = opt(x)
Kp = x(1);
Ki = x(2);
disp(x);
save('pqfile.mat','x')
options = simset ('SrcWorkspace','current','DstWorkspace','current');
out = sim('try_fit',[],options);
y=j;
end
-----------------------------------------------------------------------------------------------
Thanks & Regards
Sanjeev Gupta
  3 Comments
Sanjeev Gupta
Sanjeev Gupta on 9 Mar 2021
Hello Mathieu,
Actually, try_fit is the simulink file containing system.
and the function opt is called in Optimization Toolbox (Genetic Algorithm Solver) to optimize gains Kp and Ki of the system.
And variable x has 2 values per iteration (Kp and Ki)..
And I want to save all pairs of Kp and Ki (x(1) and x(2)) generated during optimization.
Mathieu NOE
Mathieu NOE on 10 Mar 2021
hello again
so to me x is the ouput of another function ? (from the Optimization Toolbox )
in that case you can have a code that concatenates the previous x pair with a new one like :
% init
X = [];
% maybe here for or while loop
% your Optimization Toolbox funtion here
x = stuff(...); %
y = opt(x); % where is y used - in the above function ?
X = [X; x(:)]; % concatenation of previous and current x values
% end of for / while loop
does it make sense to you ?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 9 Mar 2021
function y = opt(x)
Kp = x(1);
Ki = x(2);
disp(x);
filename = 'pqfile.mat';
if ~exist(filename)
pqsave.x = [];
else
pq = load(filename, 'x');
pqsave.x = pq.x;
end
pqsave.x(end+1) = x;
save(filename, '-struct', pqsave);
options = simset ('SrcWorkspace','current','DstWorkspace','current');
out = sim('try_fit',[],options);
y=j;
end
This is not efficient. More efficient would be
function y = opt(x)
persistent pqsave
if isempty(pqsave); pqsave.x = []; end
Kp = x(1);
Ki = x(2);
disp(x);
filename = 'pqfile.mat';
pqsave.x(end+1) = x;
save(filename, '-struct', pqsave);
options = simset ('SrcWorkspace','current','DstWorkspace','current');
out = sim('try_fit',[],options);
y=j;
end
but even more efficient would be
function [y, saved_x] = opt(x)
persistent pqsave
if isempty(pqsave); pqsave.x = []; end
if nargout > 1
y = [];
saved_x = pqsave.x;
return
end
Kp = x(1);
Ki = x(2);
disp(x);
pqsave.x(end+1) = x;
options = simset ('SrcWorkspace','current','DstWorkspace','current');
out = sim('try_fit',[],options);
y=j;
end
To use this, call opt as usual with a single output. Then after that is all finished, call it once with two outputs (no inputs needed). Ignore the first output, and the record of the x values will be in the second output. This avoids doing all the save() operations.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!