Undefined operator './' for input arguments of type 'cell'.

1 回表示 (過去 30 日間)
NN
NN 2021 年 4 月 23 日
回答済み: Ronit 2024 年 2 月 23 日
Hi , i am using the below command and getting the error:
date_num2 = datenum(testdates)+table2array(Data(1:end,2))./24;
Error
Undefined operator './' for input arguments of type 'cell'.
please help
  3 件のコメント
Md Abdur Rahman
Md Abdur Rahman 2021 年 4 月 23 日
Try this:
date_num2 = datenum(testdates)+table2array{idx}(1:end,2)./24; % idx is index of Data in table2array cell
Scott MacKenzie
Scott MacKenzie 2021 年 4 月 23 日
NN, you didn't say anything about the organization of the Data table. The problem, it seems to be, is that the 2nd column of the Data table likely contains text. The array2table function therefore produces a cell array of character strings. Hence the error message.
If the 2nd column of the Data table contains numeric data, the array2table function would yield a numeric vector and there would be no error message.

サインインしてコメントする。

回答 (1 件)

Ronit
Ronit 2024 年 2 月 23 日
Hi,
I understand you're encountering an error due to the use of the element-wise division operator ‘./’ with a cell array in MATLAB.
As it is mentioned in the comments as well, this issue arises because MATLAB's ‘./’ operator is designed for numerical arrays and cannot be directly applied to cell arrays.
It seems like the problem maybe arising when you extract data from a table using table2array, particularly the column Data(1:end,2), it returns a cell array. If the cells within Data(:,2) contain numeric values, these must be converted into a numeric array format before you can perform any element-wise division
To resolve this, it's essential to work with numeric arrays for such arithmetic operations. You can use cell2mat to convert a cell array of numeric values to a numeric array.
Here’s how it can be resolved:
% First, convert the cell array to a numeric array if the cells contain numeric data.
if iscell(Data{:,2})
numericColumn = cell2mat(Data{:,2}); % Convert cells to numeric array
else
numericColumn = table2array(Data(:,2)); % If the data is already numeric, just convert to an array
end
% Now perform the division by 24 and add to the serial date numbers from 'datenum'
date_num2 = datenum(testdates) + numericColumn ./ 24;
Please refer to the following documentations for further details:
Hope this helps!

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by