MATLAB: Mat2str issue in an embedded MATLAB Function

arrayembedded functionlarge integermat2strmatlab codersimulink

Hi, I'm working on Simulink and transferring large integers between 2 MATLAB Function blocks . Inside the receiver block, when I use the 'mat2str' function (to convert from the sent matrix to string) I get this error :
MATLAB Function Interface Error: Class mismatch for variable 'mat2str'. Expected 'double', Actual 'char'. Block Triggered Subsystem/MATLAB Function2
Even if the function's input array is initialized as double type, in this way :
X = zeros(1,168,'double');
However,when I don't initialize the input array, I don't get this error but instead I get the following one when I try to access and/or just display a portion of the outputted array (Xi6 = Xi6(2:165);):
"Subscripting into an mxArray is not supported."
I found that Mike Hosea already answered this question in a topic from 2011 ( Subscripting into an mxArray issue Answer ) and said that it's necessary to pre-define its size and type of variables, which I did in my first approach.
I wish I explained the problem clearly, any suggestion to get around this issue would be really appreciated as I'm stuck here for days, thanks.
Remark : I don't get any of those errors when I write the code on a simple MATLAB script (not embedded MATLAB in Simulink).
Here is my code :
function Z_Gold = Golden_Model(sim_time,X,Y)
%#codegen
Xi = zeros(24,1);
Xi2 = zeros(21,1);
Xi22 = zeros(1,21);
Xi3 = zeros(21,8);
Xi4 = zeros(168,1,'double');
Xi44 = zeros(1,168,'double');
%Xi5 = zeros(1,170);
%Xi5 = zeros(337,1,char);% Output class, specified as 'double', 'single', 'int8',...,
%Xi6 = zeros(1,170);
%Xi6 = zeros(1,164);
%Xi7 = '0000';
Yi = zeros(24,8);
X_g = zeros(21,1);
Y_g = zeros(21,1);
Z_Gold = 0;
coder.extrinsic('disp');
coder.extrinsic('sprintf');
coder.extrinsic('mat2str');
coder.extrinsic('strcat');
coder.extrinsic('str2num');
coder.extrinsic('vec2str');
coder.extrinsic('sscanf');
i20 = 0.4e-06 ;
if sim_time == i20
Xi = bi2de(de2bi(X,8));
disp('Xi = '); disp(Xi);disp('size Xi = '); disp(size(Xi));
Xi2 = Xi(3:23); % extract submatrix from matrix
disp('Xi2 = '); disp(Xi2); disp(size(Xi2));
Xi22 = Xi2'; disp('Xi2 = '); disp(Xi2); % transpose
Xi3 =de2bi(Xi22); disp('Xi3 = '); disp(Xi3);
Xi4 = double(Xi3'); Xi44 = double(Xi4(:)'); disp('Xi4 = '); disp(Xi4);
disp('Xi44 = '); disp(Xi44);
%Xi44 = double(Xi44);
Xi5 = mat2str(Xi44); % mat2str input should be of type double
%Xi5 = vec2str(Xi44);
disp('Xi5 = '); disp(Xi5);
%disp(mat2str(Xi44));
%Xi5 = Xi5(2:end-4);
%disp('Xi5_2= '); disp(Xi5);
Xi6 = sscanf(Xi5,'%s');
%Xi6 = Xi5(Xi5~=' ');
disp('Xi6 = '); disp(Xi6);
%Xi6 = Xi6(2:165);
%Xi7 = Xi6(2:end-1);
end
end
Remark2 : this code is working without errors and displays me the array of bits I was expecting, but still incomplete as I need to further modify this array X6 and remove from it the brackets and the 4 last 0 bits, and that's where I have the errors I'm talking about.

Best Answer

The function mat2str returns a string in MATLAB. Following Mike's answer, the variable to which the output of mat2str is assigned should be pre-initialized to be a string of the right size rather than a numeric array:
Xi5 = blanks(n,m);
Xi5 = mat2str(Xi44);
If the output size of mat2str is unclear, you may need to also use coder.varsize to force Xi5 to be variable-sized.