MATLAB Answers

Correctly define the dimension for a timeseries of scalars

13 views (last 30 days)
Luis Ruiz
Luis Ruiz on 6 Sep 2018
Answered: mbvoyager on 7 Sep 2018
I want to read data from a mat file in Simulink. To create the data file and the bus I am using timeseries objects.
dt = 0.1;
tend = 24;
t = 0:dt:24;
tk = size( t, 2 );
matrix_in = repmat( eye(3), 1, 1, tk );
scalar_in = t;
vector_in = randn( 3, tk );
vector_in_1 = vector_in;
vector_in_2 = vector_in;
vector_in_1( vector_in_1 <= 0.5 ) = 1;
vector_in_1( vector_in_1 > 0.5 ) = 0;
vector_in_2( vector_in_2 >= -0.5 ) = 1;
vector_in_2( vector_in_2 < -0.5 ) = 0;
vector_in = vector_in_1 + vector_in_2;
matrix_ts = timeseries( matrix_in, t, 'name', 'matrix_data' );
vector_ts = timeseries( vector_in, t, 'name', 'vector_data' );
scalar_ts = timeseries( scalar_in, t, 'name', 'scalar_data' );
matrix_ts = delsample( matrix_ts, 'Value', t( t >= 10 & t <= 15 ) );
vector_ts = delsample( vector_ts, 'Value', t( t >= 18 & t <= 20 ) );
scalar_ts = delsample( scalar_ts, 'Value', t( t >= 7 & t <= 11 ) );
data_struct = struct( 'matrix_in', matrix_ts, ...
'vector_in', vector_ts, ...
'scalar_in', scalar_ts );
save 'my_data' 'data_struct' -v7.3;
my_bus = Simulink.Bus.createObject( data_struct );
Then I want to read it in Simulink. So, I have a matrix a vector and a scalar, the problem I am having is that the scalar has dimension [1 1] not 1, and because of that Simulink treat it as a matrix/vector and Simulink does not allow me to do some operations. So my question is, how can I fix the dimension for the scalar to be 1 and not [1 1]. First run the code above and then the Simulink file.

  2 Comments

mbvoyager
mbvoyager on 6 Sep 2018
I have no clue about Simulink.
But what I can see is that you are actually passing three objects of the type timeseries in a structure object. One single structure object is always represented as [1x1 objectclassname] is that your issue?
Or is your issue that
size(scalar_ts.Data)
returns
ans =
1 1 241
which is a a [1x1x241] matrix.
If so you could just do
scalar_ts.Data = squeeze(scalar_ts.Data);
to have a [1x241]

Sign in to comment.

Accepted Answer

mbvoyager
mbvoyager on 7 Sep 2018
size(scalar_ts.Data)
returns
ans =
1 1 241
which is a a [1x1x241] matrix.
If so you could just do
scalar_ts.Data = squeeze(scalar_ts.Data);
to have a [1x241]
You are welcome. ;-)

  0 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by