python - 如何在 Simulink 的 MATLAB 函数中索引数组值?

标签 python arrays matlab indexing simulink

我在 simulink 中使用 matlab 函数来调用 python 脚本,该脚本根据输入值进行一些计算。 python 脚本给了我一个字符串返回给 matlab 函数,我将其拆分为一个数组。拆分后的字符串必须始终是具有 6 个变量字符串的元胞数组:

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

要使用特定的 m 文件调用 strsplit 或 python 脚本本身等函数,我正在使用 coder.extrinsic('*') 方法。

现在我想索引到一个特定的值,例如使用 dataStringArray(3) 来获取 '20.0' 并将其定义为 matlab 函数的输出值,但这不起作用!我尝试使用 dataStringArray = cell(1,6); 预定义 dataStringArray,但总是出现相同的 4 个错误:

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.

我哪里错了?

示例代码

输出定义后面的注释代码是我需要的。:

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));

解决方案 1

元胞数组在 Matlab 函数 block 中不受支持,只有本地 Simulink 数据类型是可能的。

解决方法是将整个代码定义为普通函数,并从使用 extrinsic 定义的 MATLAB 函数执行它。在执行外部函数之前,使用已知类型和大小初始化输出变量很重要。

解决方案 2

另一种解决方案是使用 strfind 函数,它会为您提供一个带有分隔字符位置的双矩阵。这样,您就可以只返回所需的字符位置范围。在这种情况下,您的整个代码将位于 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));

最佳答案

当使用外部函数时,返回的数据类型为 mxArray,您无法按照错误消息提示对其进行索引。要解决此问题,您首先需要初始化感兴趣的变量以将它们转换为正确的数据类型(例如 double )。有关如何执行此操作的示例,请参阅文档中的 Working with mxArrays

错误消息的第二部分是维度。如果没有看到函数的代码、Simulink 模型以及函数的输入/输出是如何定义的,很难判断发生了什么,但您需要确保在 Ports and Data manager 中定义了正确的大小和数据类型。

关于python - 如何在 Simulink 的 MATLAB 函数中索引数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30884370/

相关文章:

python - 无法构建轮子 - 错误 : invalid command 'bdist_wheel'

php - 对象的array_unique?

matlab - 如何绘制传递函数数组的波特图?

matlab - 分组条形图上方的标签

python - SymPy:使用 2-D 输入进行lambdaify

python - 使用 SVD 进行降维,指定要保持的能量

javascript - lodash 在 JavaScript 中对对象数组进行排序

ios - Core Image Kernel Language 的 OpenGL 坐标系

python importlib 在守护进程上下文中找不到模块

java - 如何在android中单击按钮时将值添加到数组