How can I can I export a matrix to another function in Simulink?

6 views (last 30 days)
I am trying to save this matrix in an array.
Matrix : [1,2,3,4,5,6;7,8,9,10,11,12;13,14,15,16,17,18;19,20,21,22,23,24]
size = 1
Matlab function
function y = fcn(u, size)
l = length(u);
cycles = fix(l/size);
init = 1;
for i=1:cycles
data(i)= u(init:size,:);
init = size
size = size + 1;
y = data;
data(i) --> is an array with multiple cells of size 1 x 4
[cell 1x6] [cell 1x6] [cell 1x6] [cell 1x6]
[1,2,3,4,5,6] [7,8,9,10,11,12] [13,14,15,16,17,18] [19,20,21,22,23,24]
I want to set as output the first cell which is : [1,2,3,4,5,6]
How can I do this?

Answers (1)

Kavya Vuriti
Kavya Vuriti on 20 May 2020
Fron the question, I understood that you are trying to convent a matrix of size 4x6 into cell array of size 1x4. From the given code, I understood that the variable "size" is used to specify the number of elements in each cell.If that is the case,value of size must be 6 according to your question. In the model, I think you are expecting the output of first matlab function block to be [1 2 3 4 5 6]. You could try the following code:
function y = fcn(u, size)
l = length(u);
cycles = fix(l/size);
data = cell(1, cycles);
for i=1:cycles
data{i}= u(i,:);
y = data{1};
This output of function block 'MATLAB Function' can be directly passed to the function block 'MATLAB Function 1'. Also, care must be taken in setting properties of the input and output ports of the function blocks in Model Explorer.


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!

Translated by