Is there a way to deep copy Simulink.SimulationInput object?

3 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2021 年 3 月 8 日
回答済み: MathWorks Support Team 2021 年 3 月 8 日
I am trying to copy a Simulink.SimulationInput object by doing the following:
Input(1) = Simulink.SimulationInput('foo');
Input(1) = Input(1).setVariable('A',Simulink.Parameter(5));
Input(1) = Input(1).setVariable('simin',timeseries([0,10],[0,10]));
%%
Input(2) = Input(1);
Input(2) = Input(2).setVariable('A.Value',10);
%%
Input(1).Variables(1).Value
Input(2).Variables(1).Value
By setting the value of variable 'A' to 10 after copying Input(1) to Input(2), the value for Input(1) also changes to 10. Why does this happen?
Is there a deep copy method for SimulationInput object?
 

採用された回答

MathWorks Support Team
MathWorks Support Team 2021 年 3 月 8 日
The cause of the observed behavior is that Simulink.Parameter is a handle object so when the second SimulationInput object is assigned to be equal to the first one, both of them refer to the same Simulink.Parameter object. 
There are two ways to implement the desired workflow where each SimulationInput object would have independent copies of the Simulink.Parameter object, A:
 
1. Use 'Input(2).setVariable('A',Simulink.Parameter(10))' instead of 'Input(2).setVariable('A.Value',10)', it gives the desired outcome:
Input(1) = Simulink.SimulationInput('mdlname');
Input(1) = Input(1).setVariable('A',Simulink.Parameter(5));
Input(1) = Input(1).setVariable('simin',timeseries([0,10],[0,10]));
%%
Input(2) = Input(1);
Input(2) = Input(2).setVariable('A',Simulink.Parameter(10));
%%
Input(1).Variables(1).Value
Input(2).Variables(1).Value
2.Create Simulink Parameter and SimulationInput object first, then use setVariable:
 Input(1:2) = Simulink.SimulationInput('mdlname');
A = Simulink.Parameter(5);
Input(1) = Input(1).setVariable('A', A);
Input(1) = Input(1).setVariable('simin',timeseries([0,10],[0,10]));
%%
Input(2) = Input(2).setVariable('A.Value', 10);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSchedule Model Components についてさらに検索

タグ

タグが未入力です。

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by