mat2str issue in an embedded MATLAB Function

4 views (last 30 days)
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.
  1 Comment
Edgar Zakarian
Edgar Zakarian on 28 Jun 2014
To help you understand the code, this is an overview from the outputs I'm getting. So far, That's OK for me but I can't do anything further than displaying Xi6.
Xi2 =
67
94
1
149
68
159
53
192
54
34
160
155
191
27
140
125
255
10
134
11
14
Xi3 =
1 1 0 0 0 0 1 0
0 1 1 1 1 0 1 0
1 0 0 0 0 0 0 0
1 0 1 0 1 0 0 1
0 0 1 0 0 0 1 0
1 1 1 1 1 0 0 1
1 0 1 0 1 1 0 0
0 0 0 0 0 0 1 1
0 1 1 0 1 1 0 0
0 1 0 0 0 1 0 0
0 0 0 0 0 1 0 1
1 1 0 1 1 0 0 1
1 1 1 1 1 1 0 1
1 1 0 1 1 0 0 0
0 0 1 1 0 0 0 1
1 0 1 1 1 1 1 0
1 1 1 1 1 1 1 1
0 1 0 1 0 0 0 0
0 1 1 0 0 0 0 1
1 1 0 1 0 0 0 0
0 1 1 1 0 0 0 0
...
Xi5 =
[1 1 0 0 0 0 1 0 0 1 1 1 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0 1 0 1 1 1 1 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 1 1 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 0 0 1 1 1 1 1 1 1 0 1 1 1 0 1 1 0 0 0 0 0 1 1 0 0 0 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 0 0 1 1 1 0 0 0 0]
Xi6 =
[110000100111101010000000101010010010001011111001101011000000001101101100010001000000010111011001111111011101100000110001101111101111111101010000011000011101000001110000]

Sign in to comment.

Accepted Answer

Ryan Livingston
Ryan Livingston on 28 Jun 2014
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.

More Answers (1)

Edgar Zakarian
Edgar Zakarian on 28 Jun 2014
Hi Ryan, Thanks very much really, how careless of me ! Now it works like a charm, :)
Here is the entire clean 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');
Z_Gold = 0;
Done = 0;
coder.extrinsic('disp');
coder.extrinsic('sprintf');
coder.extrinsic('mat2str');
coder.extrinsic('strcat');
coder.extrinsic('str2num');
coder.extrinsic('vec2str');
coder.extrinsic('bin2vpi');
coder.extrinsic('vpi');
i20 = 0.4e-06 ;
if sim_time == i20
disp(sim_time);
disp('X_gold = '); disp(X);
disp('size X = '); disp(size(X));
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);
Xi5 = blanks(337);
Xi5 = mat2str(Xi44);
disp('Xi5 = '); disp(Xi5);
Xi6 = Xi5(Xi5~=' ');
disp('Xi6 = '); disp(Xi6);
Xi6 = Xi6(2:165)
Xi7 = bin2vpi(Xi6); disp('Xi7 = '); disp(Xi7);
end
end

Community Treasure Hunt

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

Start Hunting!