Why I get a 3 dimensional array using to workspace block?

14 views (last 30 days)
Hello everybody. I have a for loop which its output is 2 by N. N can be 1000 for example. And I define this for loop inside matlab function block in simulink. When I want to get output from it, it is defined as 2 by 1000 by 51. I mean it becomes 3 Dimensional! Why this happens and how to solve it? I also saw this problem when I wanted to integrate a matlab function. I need the output be M by N, but not M by N by D.
  4 Comments
Torsten
Torsten on 3 Oct 2024
Edited: Torsten on 3 Oct 2024
Can you think of a reason for the number 51 as the third dimension ? Maybe some variable that is set to have 51 elements ?
What is input to the function block ?
Faezeh Yousefpour
Faezeh Yousefpour on 3 Oct 2024
I didn't define any input to the function. Once I put clock block and to workspace block, time length was 1 by 51! I used 2D array in to workspace block, still I got 3D array! It is related to time! I even defined t as input to the function, again I received 3D array!

Sign in to comment.

Answers (1)

Shivam Gothi
Shivam Gothi on 4 Oct 2024
As per my understanding, you are trying to log the 2d array which is an output from MATLAB function block. But instead of being a 2D array, it is a 3D array.
Here, the third dimension is corresponding to the “time step”. That is, the 2D array may have different values at different simulation time-steps. Therefore, Simulink exports the value of 2D array at every time-step, thus making it a 3D array. For an example, suppose that the MATLAB function outputs a (2 x 3) array and simulation takes 5 time steps, then your logged data will be (2 x 3 x 5). As depicted in below figure. In your case, it is (2 x 1000 x 51), because the simulation takes 51 steps.
You cannot get rid of it. But, as a work-around, you can extract the 2D array at time step (N), from the 3D array by typing the below command in MATLAB command window.
Two_D_Array = simout(:,:,N);
% This will extract the 2D matrix at Nth time step, which is the output of
% MATLAB function.
The below given figure shows the worksace variables. We can see that the variable "Two_D_Array" is extracted and has dimention (2 x 1000)
(Note: here, the data is exported as “Array” (look inside the “save format” field found in “block parameters” of “simout” block)
I have attached the demo Simulink model containing a “MATLAB function” block, which outputs a (2 x 1000) matrix.
I hope this helps !

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!