2

I'm using a matlab-function in simulink to call a python script, that do some calculations from the input values. The python-script gives me a string back to the matlab-function, that I split to an array. The splitted string has always to be a cell array with 6 variable strings:

dataStringArray = '[[-5.01 0.09785429][-8.01 0.01284927]...'    '10.0'    '20.0'    '80.0'    '80.0'    '50.0'

To call the functions like strsplit or the python-script itself with a specific m-file, I'm using coder.extrinsic('*') method.

Now I want to index to a specific value for example with dataStringArray(3) to get '20.0' and define it as an output value of the matlab-function, but this doesn't work! I tried to predefine the dataStringArray with dataStringArray = cell(1,6); but get always the same 4 errors:

Subscripting into an mxArray is not supported.
Function 'MATLAB Function' (#23.1671.1689), line 42, column 24:
"dataStringArray(3)"

2x Errors occurred during parsing of MATLAB function 'MATLAB Function'

Error in port widths or dimensions. Output port 1 of 's_function_Matlab/MATLAB Function/constIn5' is a one dimensional vector with 1 elements.

What do I'm wrong?

SAMPLE CODE

The commented code behind the output definitions is what I need.:

function [dataArrayOutput, constOut1, constOut2, constOut3, constOut4, constOut5] = fcn(dataArrayInput, constIn1, constIn2, constIn3, constIn4, constIn5)

coder.extrinsic('strsplit');

% Python-Script String Output
pythonScriptOutputString = '[[-5.01 0.088068861]; [-4.96 0.0]]|10.0|20.0|80.0|80.0|50.0';
dataStringArray          = strsplit(pythonScriptOutputString, '|');

% Outputs
dataArrayOutput = dataArrayInput; % str2num(char((dataStringArray(1))));
constOut1       = constIn1;       % str2double(dataStringArray(2));
constOut2       = constIn2;       % str2double(dataStringArray(3));
constOut3       = constIn3;       % str2double(dataStringArray(4));
constOut4       = constIn4;       % str2double(dataStringArray(5));
constOut5       = constIn5;       % str2double(dataStringArray(6));

SOLUTION 1

Cell arrays are not supported in Matlab function blocks, only the native Simulink datatypes are possible.

A workaround is to define the whole code as normal function and execute it from the MATLAB-Function defined with extrinsic. It`s important to initialize the output variables with a known type and size before executing the extrinsic function.

SOLUTION 2

Another solution is to use the strfind function, that gives you a double matrix with the position of the splitter char. With that, you can give just the range of the char positions back that you need. In this case, your whole code will be in the MATLAB-Function block.

function [dataArrayOutput, constOut1, constOut2, constOut3, constOut4, constOut5] = fcn(dataArrayInput, constIn1, constIn2, constIn3, constIn4, constIn5)

coder.extrinsic('strsplit', 'str2num');

% Python-Script String Output
pythonScriptOutputString = '[[-5.01 0.088068861]; [-4.96 0.0]; [-1.01 7.088068861]]|10.0|20.0|80.0|80.0|50.0';
dataStringArray = strfind(pythonScriptOutputString,'|');

% preallocate
dataArrayOutput = zeros(3, 2);
constOut1       = 0;
constOut2       = 0;
constOut3       = 0;
constOut4       = 0;
constOut5       = 0;

% Outputs
 dataArrayOutput = str2num(pythonScriptOutputString(1:dataStringArray(1)-1));
 constOut1       = str2num(pythonScriptOutputString(dataStringArray(1)+1:dataStringArray(2)-1));
 constOut2       = str2num(pythonScriptOutputString(dataStringArray(2)+1:dataStringArray(3)-1));
 constOut3       = str2num(pythonScriptOutputString(dataStringArray(3)+1:dataStringArray(4)-1));
 constOut4       = str2num(pythonScriptOutputString(dataStringArray(4)+1:dataStringArray(5)-1));
 constOut5       = str2num(pythonScriptOutputString(dataStringArray(5)+1:end));

1 Answer 1

2

When using an extrinsic function, the data type returned is of mxArray, which you cannot index into as the error message suggests. To work around this problem, you first need to initialise the variable(s) of interest to cast them to the right data type (e.g. double). See Working with mxArrays in the documentation for examples of how to do that.

The second part of the error message is a dimension. Without seeing the code of the function, the Simulink model and how the inputs/outputs of the function are defined, it's difficult to tell what's going on, but you need to make sure you have the correct size and data type defined in the Ports and Data manager.

Sign up to request clarification or add additional context in comments.

9 Comments

Thx for fast response! I read the documentation and tried to initialise the variabels but it doesn't work for a string array with the cell() method. When I write "dataStringArray = ones(1,6);" for example, then the functions compiles but I get a runtime error because of wrong datatyp of course. When I comment out the indexing line "dataStringArray(3)", I get no errors.
Again, you don't share your code so it's difficult to find out what's going on, but by the sound of dataStringArray = ones(1,6); will initialise the data to an array of doubles, not strings. For strings, you probably want to try something like dataStringArray = '';
I added a base code sample to my question. I tried dataStringArray = ''; but get error Index expression out of bounds. When I initialise it with dataStringArray = '123456'; then I get another errors. I'm really confused now.
What error do you get when initialising dataStringArray to '123456'? Is it always of size 6?
It has to be a cell array with always 6 variable size strings. So something like dataStringArray = cell(1,6) but this not working and I get the errors mentioned in my question. When I initialize it with dataStringArray = '123456' I get MATLAB expression 'strsplit' is not numeric. error.
|

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.