The MATLAB Function Block can return arrays. The problem with your code is probably just that you can't return the persistent array. If you create an array UB = U and return UB instead of U, it should work.
As for the synchronization, there may be more direct ways of solving the problem that I don't know much about (tunable parameters?), but I think it should be possible to create a MATLAB function with persistent (or global) variables that acts as a repository. Something like
function [a,b,c] = myrepository(a,b,c)
persistent asaved bsaved csaved
if isempty(asaved)
asaved = 0;
bsaved = 0;
csaved = 0;
end
if nargin == 0
a = asaved;
b = bsaved;
c = csaved;
else
asaved = a;
bsaved = b;
csaved = c;
end
That's a MATLAB function that lives in MATLAB somewhere on the path, not in your MATLAB function block. Then in your MATLAB function block you retrieve the data
[a,b,c] = feval('myrepository');
and set the data
feval('myrepository',a,b,c);
or declare it extrinsic near the beginning of your function
coder.extrinsic('myrepository');
and get/set the data with
[a,b,c] = myrepository;
myrepository(a,b,c);
Of course you'll need to be sure to initialize the data in myrepository as appropriate before the simulation begins.
Best Answer