1

I would like to transmit an encoded image using USRP. The first step is to load the image using Matlab and encode it. The respective codes are shown as follows.

function msg = genMsg1
%#codegen
persistent msgStrSet count;
if isempty(msgStrSet)
  count = 0;
  msgStrSet = imread('cameraman.tif'); % Load the image of cameraman.tif
end
msgtemp = dec2bin(msgStrSet); % Covert msgStrSet into binary value
msg_1ine = msgtemp(count+1,:).'; % Take msgtemp line by line and tranpose it
msg = str2num(msg_1ine);  % Convert each line from string into column vector
count = mod(count+1,65536);
end

And the running result of this M-file is: ans =

 1
 0
 1
 0
 0
 0
 0
 0

Since I should use the block of SDRU transmitter, I have to encode above codes into a matlab function, which is shows as following figure. enter image description here

But when I run this block, error windows pops up, as the following figure shows.

enter image description here

The first error is:

Subscripting into an mxArray is not supported.
Function 'MATLAB Function' (#46.311.329), line 11, column 12:
"msgtemp(count+1,:)"
Launch diagnostic report.

The second error is

Undefined function or variable 'msg_1ine'. The first assignment to a local variable determines its class.
Function 'MATLAB Function' (#46.391.399), line 12, column 15:
"msg_1ine"
Launch diagnostic report.

The third error and fourth error are the same.

Errors occurred during parsing of MATLAB function 'MATLAB Function'(#45) 

I think the second, third and fourth errors are due to the first error,

Subscripting into an mxArray is not supported.

I have search the internet for the whole day, and still could not find the similar problem. Could anyone tell me what is "Subscripting into an mxArray is not supported" after all and how to solve it?

Thanks in advance for any leading!

2 Answers 2

2

I don't think imread is supported for code generation, see Functions and Objects Supported for C and C++ Code Generation, so you need to declare it as extrinsic. I suspect this is what you have done in your block, even though you don't mention it in your code. The problem is when a function is declared as extrinsic, the data type it returns is mxArray, see Call MATLAB Functions, and in particular the "Working with mxArrays" section.

The workaround is to initialise your msgStrSet variable to 0, to force MATLAB Coder to set the variable data type to double (or anything other than mxArray):

function msg = genMsg1
%#codegen
coder.extrinsic('imread'); % declare imread as extrinsic
persistent msgStrSet count;
if isempty(msgStrSet)
  count = 0;
  msgStrSet = 0; % define msgStrSet as a double
  msgStrSet = imread('cameraman.tif'); % Load the image of cameraman.tif
end
msgtemp = dec2bin(msgStrSet); % Covert msgStrSet into binary value
msg_1ine = msgtemp(count+1,:).'; % Take msgtemp line by line and tranpose it
msg = str2num(msg_1ine);  % Convert each line from string into column vector
count = mod(count+1,65536);
end
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. I forget to mention that I have declared 'imread' and 'str2num' as extrinsic. After initialising 'msgStrSet' to 0 as what you said, the running result shows "Function output 'msg' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type." Then I initialise 'msg' as msg = zeros(8,1) since 'msg' is expected as a matrix of 8*1, and running result shows "Class mismatch for variable 'imread'. Expected 'double', Actual 'uint8'. Block MATLAB Function (#55) While executing: State During Action ". It really puzzles me. Any help?
The error is saying that a double was expected. This is because the preinitialization value zeros(8,1) is double. imread is returning an array of uint8 so use zeros(8,1,'uint8') to preinitialize. The value used to preinitialize needs to match the expected size, complexity and type (e.g. single, double, int32 etc.).
@Jinlong: see @lilbill39's comment. Change the data type to uint8 when initialiazing msg and all should be good.
@lilbill39 I also noticed it later and I add the command "msg = uint8(msg)" which has the same function with your command. Then after running it, the error window pops up, which shows "Class mismatch for variable 'imread'. Expected 'double', Actual 'uint8'. Block MATLAB Function (#55) While executing: State During Action ".Could you pls give me some ideas?
@Jinlong: you probably need to initialise msgStrSet as uint8 as well, if imread is going to return a variable of type uint8.
0

Thanks for the help. The correct codes are written as follows.

function msg = genmsg
persistent count msgStrSet;
coder.extrinsic('imread','str2num');
if isempty(msgStrSet)
    count = 0;
    msgStrSet = zeros(256,256,'uint8'); % Intialize msgStrSet as unsigned interger matrix of 256*256
    msgSet = imread('cameraman.tif'); % Load the image of cameraman.tif 
end
msgtemp = dec2bin(msgStrSet);  % Covert msgStrSet into binary value
msg_line = msgtemp(count+1,:).' % Take msgtemp line by line and tranpose it
msg = zeros(8,1,'double'); % Intialize msg as matrix of 8*1.
msg = str2num(msg_line); % Convert each line from string into column vector
count = mod(count+1,65536);
end

3 Comments

Next time, it would be better to accept the answer that was given to you than create your own answer and then accept it.
@am304 I have cancelled accepting my answer. But your answer is not the totally correct one.
It is, based on the incomplete information in the question. The reason for the error is the fact that you have extrinsic functions in your code, giving you data of mxArray type. The correct way to deal with this type of errors is to initialise the variable to the correct data type before calling the function. The rest are implementation details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.