i want to make MATLAB function in Simulink to read one cell from excel sheet every period of time

2 次查看(过去 30 天)
function object = readFromXls(ReadFlag)
coder.extrinsic('readcell');
coder.extrinsic('str2num');
coder.extrinsic('cellfun');
coder.extrinsic('cell2mat');
data = 0;
object = 0;
if ReadFlag == 1
excelFilePath = 'YOLO_Object_Type.xlsx';
% Read data from the Excel file
data = cell2mat(excelFilePath);
%iwant = cellfun(@cell2mat,data)
object = data;
else
object =0;
end
  1 个评论
Ahmad Alswalhi
Ahmad Alswalhi 2024-5-3
编辑:Walter Roberson 2024-5-4
the error :
Brace indexing is not supported for variables of this type. Error in cell2mat.m (line 42) cellclass = class(c{1}); Error in 'untitled/MATLAB Function' (line 18)
how can i fix that ?
or is the other way to do this function?

请先登录,再进行评论。

回答(1 个)

Pavan Sahith
Pavan Sahith 2024-5-7
编辑:Pavan Sahith 2024-5-7
I see that you are trying to import data from an Excel file processing within a MATLAB function block but it seems you've encountered an error along the way.
The error message you're seeing, "Brace indexing is not supported for variables of this type," is due to an incorrect use of the 'cell2mat' function on a string variable (excelFilePath) rather than on a cell array.
The ' cell2mat ' function is designed to convert cell arrays of numeric values or strings into a regular numeric array or character array, respectively. However, in your code, you are attempting to use ' cell2mat ' on a string that represents the file path, which is not a cell array.
To fix the error,using the ' readcell ' function can help to read the contents of the Excel file into a cell array.
data = readcell(excelFilePath);
The readcell function reads the Excel file and stores its content in a cell array named data.
The following MathWorks documentation helps you to know more about ' readcell ' and ' cell2mat '
Hope this helps you in moving forward

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by