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

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 comentarios

Why this happens and how to solve it?
You gave us too little information to be able to answer this.
This is my main code which I used in matlab function block. The output is Yt
dt = 0.005; %sample time
t = 0:0.01:2;
N = length(t);
Q1 = 9e-4;
Q5 = 1e-5;
R1 = 0.1;
R2 = 0.01;
Q = diag([Q1, Q5]);
Q6 = diag([Q1, 0,0,0,Q5]);
R = diag([R1, R2]);
ms = 1242;
mu = 100;
k1s = 80000;
k2s = 32000;
c1s = 4000;
c2s = 1600;
kt = 405000;
ms = 1242;
mu = 100;
k1s = 80000;
k2s = 32000;
c1s = 4000;
c2s = 1600;
deltacs = 775;
csbar = 2800;
kt = 405000;
g = 9.81;
xt = zeros(5,N);
yt = zeros(2,N);
for i = 1:N-1
xt(:,i+1) = xt(:,i) + 0.005*(PASSIVEF(xt(:,i)));
yt(:,i+1) = (MeasurementMatrix(xt(:,i))) ;
end
Yt = yt;
%%
And these are functions defined inside the code
function y = MeasurementMatrix(x)
ms = 1242;
mu = 100;
k1s = 80000;
k2s = 32000;
k2 = 32000;
c1s = 4000;
c2s = 1600;
deltacs = 775;
csbar = 2800;
kt = 405000;
Fa = 2100;
V = 20;
ds0 = -0.1590;
dt0 = -0.0325;
g = 9.81;
x1 = x(1);
x2 = x(2);
x3 = x(3);
x4 = x(4);
x5 = x(5);
cs = deltacs*atan(1000*(x(4)-x(3)) +csbar);
y1 =( kt*x1+k1s*(x5-x2) + k2s*(x5-x2)^3 -k1s*x5-k2s*x5^3 +cs*(x4-x3))/mu;
y2 = g*(-k1s*(x5-x2) -k2s*(x5-x2)^3 +k1s*x5+k2s*x5^3 ...
-cs*(x4-x3))/(-k1s*x5-k2s*x5^3);
y = [y1;y2];
end
%%
function x = PASSIVEF(x)
dt = 0.005;
Q1 = 9e-4;
Q5 = 1e-8;
R1 = 0.1;
R2 = 0.01;
Q = diag([1 1 1 1 1]);
R = diag([R1, R2]);
Qn = [Q1 0; 0 Q5];
ms = 1242;
mu = 100;
k1s = 80000;
k2s = 32000;
c1s = 4000;
c2s = 1600;
kt = 405000;
ms = 1242;
mu = 100;
k1s = 80000;
k2s = 32000;
c1s = 4000;
c2s = 1600;
deltacs = 775;
csbar = 2800;
kt = 405000;
Fa = 2100;
Vlc = 20;
ds0 = -0.1590;
dt0 = -0.0325;
g = 9.81;
x1 = x(1); x2 = x(2); x3 = x(3); x4 = x(4); x5 = x(5);
cs = deltacs*atan(1000*(x4-x3)) +csbar;
numerator1 = kt*x1 + k1s*(-0.15 - x2) +k2s*(-0.15-x2)^3 +ms*g ;
numerator2 =cs*(x4-x3);
x =[-x3;
x3 - x4;
(numerator1+numerator2)/mu;
((-k1s*(-0.15-x2) - k2s*(-0.15-x2)^3 +ms*g -cs*(x4-x3)))/ms;
0] ;
x = x +dt*x;
end
Torsten
Torsten el 3 de Oct. de 2024
Editada: Torsten el 3 de Oct. de 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 ?
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!

Iniciar sesión para comentar.

Respuestas (1)

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 !

Preguntada:

el 3 de Oct. de 2024

Respondida:

el 4 de Oct. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by