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

12 vues (au cours des 30 derniers jours)
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:

Réponses (1)

Tejas
Tejas le 14 Août 2024
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:

Catégories

En savoir plus sur Simulink Functions dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by