Simulink Mathlab Function Block Output Variable Size Array

23 views (last 30 days)
Semih
Semih on 25 Dec 2024 at 8:52
Commented: Semih on 26 Dec 2024 at 7:24
Hi,
I encountered the problem about array size from matlab function block. First of all I was creating a simulink project about data control. Its include some input values (constant blocks), mathlab function and display block. The input data is the address value, the length of the data to be transmitted and other values. Also, all the value type is uint8. Matlab function block is doing generate new array for output data. This array length is variable (regValue). For example lets assume the regValue is a 3. newArray length is a equal to that. In that case newArray = [data1 data2 data3]. But this program getting failure about variable type. At the same time, pins y5 and y6 are connected to the display block. How can I do this?
Some Errors;
Block Diagram:
Code:
function [y1, y2, y3, y4, y5] = response(slaveAddr, funcCode, startAddr, regValue)
%#codegen
% slaveAddr: 1 byte (Slave adresi)
% funcCode: 1 byte (Fonksiyon kodu)
% startAddr: 2 byte (Başlangıç adresi)
% regValue: 2 byte (Register değeri veya değerleri)
% crc1, crc2: 1 byte (Her iki CRC byte'ı)
% generalData: (23 byte veri)
% Verileri başlatıyoruz
y1 = uint8(slaveAddr); % 1 byte slave address
y2 = uint8(funcCode); % 1 byte function code
y3 = uint8(regValue * 2); % 1 byte Byte Count
totalValue = (y3+2); % Başlangıçta boş veri High
y4 = zeros(1, totalValue);
y5 = zeros(1, length(y3));
end

Answers (1)

Walter Roberson
Walter Roberson on 25 Dec 2024 at 9:43
Insert
totalValue = uint8(0);
before
totalValue = (y3+2);
  4 Comments
Walter Roberson
Walter Roberson on 25 Dec 2024 at 11:59
y3 = uint8(regValue * 2); % 1 byte Byte Count
totalValue = (y3+2); % Başlangıçta boş veri High
y4 = zeros(1, totalValue);
y3 is a uint8, so the maximum value it can hold is 255.
Adding 2 to a uint8 with value 255, gets 255 again. So the maximum value of totalValue is 255.
It follows that y4 is somewhere between 2 elements and 255 elements.
You need to code y4 first using coder.varsize giving an appropriate upperbound (at most 255)
Semih
Semih on 26 Dec 2024 at 7:24
Hi Walter,
It was solved. I done that you said. By adding the "%#codegen" and "coder.varsize('Y', [1, 100]);" code solve. Also I change the output parameters settings from Edit Data. This changing, size is a [1,100] and "Advanced Variable size" is marked. And I used this blok in enabled subsystem. The enabled system block was getting failure. Change the "Propagate size of variable-size signals:". I used "During execution" instead of "Only when enabling". Thank you for your help.

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!