Array manipulation issue in Simulink

Hello,
I am trying to manipulate an array in Simulink through Matlab function block, but despite trying different ways of doing, I always get an error. Basically, I have a trapeze signal of the type:
t=[0, 6, 58, 65, 85, 91, 136, 143]
v=[0, 6, 6, 0, 0, 7, 7, 0]
Then what I want to do is get just the value of t where there is a rising of "v". This will give me the matrix below:
a=[6, 91]
Because v rise at t=6 and t=91.
What I wrote below doesn't work tho:
function a=fcn(v,t)
a=zeros(1,length(v));
idx=gt(v,0);
CC=diff(idx);
CC1=[CC; 0];
CC2=CC1>0;
CC3=CC2.*t;
CC3(CC3==0)=[];
a(1:length(CC3))=CC3;
Can anyone tell me why?

 Respuesta aceptada

Birdman
Birdman el 21 de Dic. de 2017

0 votos

Well, your situation can not be properly solved by using a MATLAB Function in Simulink because you need to indicate the size of the output from the very beginning, but at the same time you try to supply the inputs and their size are changing until the simulation is over, so I came up with a solution where I supplied the t and v vectors with a proper simulation time vector to Simulink and make some manipulation with basic Math Operation blocks. At the end of the simulation, a vector you want is logged to the workspace. I also put some commands to PreLoadFcn and StopFcn in File->Model Properties->Model Properties->Callback. Hope this helps.

6 comentarios

Matt
Matt el 21 de Dic. de 2017
Editada: Matt el 21 de Dic. de 2017
Hello, thank you for your answer. However, I am using Matlab 2016b and cannot open your file. Is there a way to make it compatible with me? I am using Simulink 8.9
Birdman
Birdman el 21 de Dic. de 2017
Matt
Matt el 21 de Dic. de 2017
Thank you it works! However, I want to implement this into an already existing model so it doesn't quite work. However, what if I already know the size of the output in my previous code? How could I make it work?
Birdman
Birdman el 21 de Dic. de 2017
Can you send me your model so that I can modify and make a better explanation?
Matt
Matt el 21 de Dic. de 2017
Unfortunately I cannot do that because it is confidential, sorry. :-/
Birdman
Birdman el 21 de Dic. de 2017
At least try to look at the codes in PreLoadFcn and StopFcn and try to understand what I did. Also, if the answer helped, can you accept it?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre General Applications en Centro de ayuda y File Exchange.

Preguntada:

el 20 de Dic. de 2017

Comentada:

el 21 de Dic. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by