How to store structs and cell arrays of matrices in an S-function's Dwork memory.
1 view (last 30 days)
I implemented a sophisticated multi-model process observer in MATLAB code using struct objects for convenience. Each struct has fields for all the parameters and variables needed by the observer, including matrices, cell-arrays of matrices, and cell arrays of stuct instances representing individual Kalman filters.
Now I'm looking into whether this matlab code could be used directly in an S-function. Now I realise that structs and cell arrays are not possible data types for Dwork vectors. Would I have to extract and copy all the dynamic (i.e. time-varying) elements from these cells and structs into Dwork vectors every iteration (and then re-copy them all back into the new struct objects at the start of the next iteration)?
Or is there an easier way to implement this code in Simulink?
Example of a simulation of the observer in MATLAB:
% Simulate observer
Xk_est = nan(nT+1,n);
Yk_est = nan(nT+1,ny);
obs = MKF1;
for i = 1:nT
uk = U(i,:)';
yk = Ym(i,:)';
obs = update_MKF(obs, uk, yk);
Xk_est(i+1,:) = obs.xkp1_est';
Yk_est(i+1,:) = obs.ykp1_est';
MKF1 = obs;
To get a sense of the challenge here, each obs instance has about 15 dynamic real arrays (no problem) but also has a cell array of n_filt dynamic integer arrays, and a cell array of structs containing 4 dynmamic arrays, where n_filt is the number of 'sub-filters' which could be anywhere from 2 to up to 50 (although the number is not time-varying).
Maybe this kind of observer is too complicated to consider implementing in Simulink. Or perhaps I need to re-write the existing code without the structs and cell array data structure to implement in an S-function.
Any advice appreciated.
The observer code is here if you are interested.