How to code a time delay specified by a value saved in a parameter structure in a Matlab block?

조회 수: 7(최근 30일)
Ryan Wilson
Ryan Wilson 2022년 4월 12일
댓글: Ryan Wilson 2022년 4월 20일
Hi,
I'm having an issue trying to go about coding a time delay within a matlab block.
Say I have a matlab block function for basic mass balance, such as:
function [X1,Y1] = fcn(Z1)
% Set transfer function value(s).
A1 = 0.8;
% Calculate the products.
X1 = Z1*A1;
Y1 = Z1*(1-A1);
end
But I need the outputs [X1,Y1] to be delayed by a certain length of time (NOT paused) specified by a value in a parameter structure in the base workspace, such as:
% Example parameter structure index for time delay value.
myParams.dt(1) = 24.8
I've been looking into other feeds for using persistent variables, but my Matlab skills are not yet very advanced. This needs to be coded in for my purposes and not looking to use a predefined simulink block (e.g. transport delay, triggered subsystems, etc.).
Could someone please help me with this seemingly simple piece of code?
Thanks so much in advance!
Cheers,

답변(1개)

Fangjun Jiang
Fangjun Jiang 2022년 4월 13일
In the MATLAB Function block editor, click the "edit data" button, then add parameter. Define the "myParams" in base workspace as the parameter of the MATLAB Function block and then you will be able to use it. I am not sure if a structure has anything special, you could try a regular variable first.
  댓글 수: 5
Ryan Wilson
Ryan Wilson 2022년 4월 20일
Thanks for your continued input.
But I'm still confused - I don't think I need a rolling buffer because the arbitrary time is not changing during the simulation.
Going back to my original example, could I not insert the param structure as a parameter to the matlab function block (as you explained above), and use the called time parameter to modify the delay code? Something like:
function [X1,Y1] = fcn(Z1, myParams)
persistent Z1_d;
if isempty(Z1_d)
Z1_d = cast(myParams.dt(1), 'like', Z1)
end
% Set transfer function value(s).
A1 = 0.8;
% Calculate the products.
X1 = Z1_d*A1;
Y1 = Z1_d*(1-A1);
end
If I still need to calculate N=delayTime/simulationStepTime in my initialization routine, how do I go about doing that? My delayTime would be my 'myParams.dt(1)', but I'm not sure how to access the simulationStepTime...
Thanks again for your time!
Cheers

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by