文档

结构格式的伐木状态

This example shows how logging state trajectories of a Simulink® model in a structure format can be better than the traditional method of logging states in an array format. The ordering of the states along the columns in the logged matrix depends on the block sorted order, which the Simulink software determines during model compilation. Various factors can affect the block sorted order, which can alter the ordering of the states.

此示例说明了如何以结构格式记录状态(将块名称与状态轨迹存储的结构格式如何有助于防止状态排序问题。

以阵列格式记录的问题

默认情况下,Simulink软件金宝app以数组格式记录状态轨迹,该轨迹是带有n列的矩阵,其中n是状态数。矩阵具有M行,每行都对应于单个仿真时间步。MATLAB®易于操纵这种M-b-N矩阵形式。但是,沿记录矩阵的列的状态变量排序取决于块排序顺序。因此,任何希望在模型中的块状态和状态矩阵的列之间进行固定映射的任何MATLAB代码,当块随着模型的变化而发生变化时,矩阵的列很容易断裂。

例如,考虑以下两个框图:

MDL1='sldemo_state_logging1';mdl2 ='sldemo_state_logging2';Open_System(MDL1);Open_System(MDL2);

这两个图具有相同的块,唯一的区别是输出端口的排序。模拟模型并以数组格式记录状态:

simout1 = sim(mdl1,“ saveformat',,,,'Array');simout2 = sim(mdl2,“ saveformat',,,,'Array');

从simulink.simulationOutput对象中提取状态向量,金宝app其中包含模拟的输出日志:

x1= simOut1.get('xout');x2 = simout2.get('xout');

请注意,在两个框图中,积分器块的相对顺序是不同的。这导致登录状态x1andx2to differ because the mapping between the columns and the states is different:

isequal(x1, x2)
ans =逻辑0

使用结构格式记录

再次模拟模型,但是这次以结构格式记录状态:

simout1=sim(mdl1,“ saveformat',,,,'结构');simout2=sim(mdl2,“ saveformat',,,,'结构');

Extract the structures, which contain the state logs, from the simulation output object:

x1s = simout1.get('xout');x2s = simout2.get('xout');

显示这些结构。请注意,这些结构有两个字段:时间和信号。字段“时间”是空的,因为我们为模型参数'saveformat'选择了“结构”。我们本可以选择“结构与时间”存储在状态结构中的时间向量:

disp(x1s); disp(x2s);
时间:[]信号:[1×2结构]时间:[]信号:[1×2 struct]

该软件将状态轨迹记录到xs.signals(k).values以及块的名称xs.signals(k).blockname与这些状态相对应。将状态提取到类似的矩阵中(例如数组格式):

x1a = [x1s.signals.values];x2a = [x2s.signals.values];

请注意,国家排序问题仍然存在(x1aandx2ax1andx2,通过阵列格式获得):

iSequal(x1a,x2a)
ans =逻辑0

获得具有固定状态订单的状态矩阵

要解决状态排序问题,请使用与值一起存储的块名称将状态映射到固定顺序,例如,块名的字母顺序排序:

[~, idx1] = sort({x1s.signals.blockName}); x1 = [x1s.signals(idx1).values]; [~, idx2] = sort({x2s.signals.blockName}); x2 = [x2s.signals(idx2).values]; isequal(x1, x2)
ans =逻辑1

通过重新排序信号阵列x1andx2进入块名的字母顺序,并以该顺序将值字段提取到矩阵中x1andx2,,,,we have a mechanism for logging the states into a matrix with a fixed mapping of the block states to columns of the logged matrix.

关the models and clear the variables which were used in this example:

CLOSS_SYSTEM(MDL1);CLOSS_SYSTEM(MDL2);清除ansIDX1IDX2MDL1MDL2simout1simout2x1x1ax1sx2x2aX2S
Was this topic helpful?