Extracting string value from table - Receive error "Expression for initial value of data must evaluate to logical or supported numeric type."

21 次查看(过去 30 天)
Hello,
I'm attempting to extract a char vector from a table, convert it to string, and pass it to another block. Unfortunately, when attempting this I keep running into the error, "Expression 'test_names' for initial value of data 'test_names' must evaluate to logical or supported numeric type." I'm not sure why I keep receiving this error, as I have recreated the same model with a constant set to 1, ran it through a MATLAB function block with simpler code to assign to the output variable (such as "if (u > 0), y = "Greater than 0", else, y = "Less than or equal to 0", and displayed it with a display block, which worked as expected and displayed a string in the display block.
I have attached an example excel file to this post. Please see below for the MATLAB code I am using.
MATLAB script to extract the column from the Excel file (attached):
clear FltDesc
Test_XLRaw=readtable('Test_FaultList.xlsx');
test_names=XLRaw(:,3);
MATLAB code within the function block (test_names scope is set to "Parameter", u scope is set to "Input"):
function y = getFltDesc(u, test_names)
% This function fetches the fault description from the fault list.
% 'u' is the fault number that is an input to this function.
% The function takes u and uses it as a row number and fetches the fault
% description from the 1st column.
y = convertCharsToStrings(test_names{u,["Var3"]});
end
Test Simulink model:

回答(1 个)

Tejas
Tejas 2024-8-14
Hello Rocke,
Table-type data cannot be directly passed as an input to a MATLAB Function Block in Simulink. The table needs to be converted to a structure-type variable first before it can be accessed by the MATLAB Function Block. Here is a workaround to access the contents of a table:
  • Save the table inside a .MAT file.
  • Create a mask on the MATLAB Function Block, which includes an edit parameter to store the table and a button with an associated uigetfile callback to select the .MAT file. This callback will also be responsible for loading the .MAT file and extracting the table from it.
  • Associate a mask initialization callback with the edit parameter to convert the table to a structure parameter using the table2struct function: https://www.mathworks.com/help/releases/R2022b/matlab/ref/table2struct.html .
  • This way, the structure parameter will be accessible in the MATLAB Function Block.
For more details, refer to the following MATLAB Answer:

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by