How to transform 'double' into 'cell array'?

1 回表示 (過去 30 日間)
John
John 2017 年 1 月 9 日
編集済み: Guillaume 2017 年 1 月 9 日
My code:
B = zeros(height(A),1);
col_names = A.Properties.VariableNames; % Replicate header names
for k = 1:height(A)
% the following 'cellfun' compares each column to the values in A.L{k},
% and returns a cell array of the result for each of them, then
% 'cell2mat' converts it to logical array, and 'any' combines the
% results for all elements in A.L{k} to one logical vector:
C = any(cell2mat(...
cellfun(@(x) strcmp(col_names,x),A.L{k},...
'UniformOutput', false).'),1);
% then a logical indexing is used to define the columns for summation:
B(k) = sum(A{k,C});
end
generates the following error message. Thanks for helping out!
Error using cellfun
Input #2 expected to be a cell array, was double instead.
This is how table 'A' looks like:
  2 件のコメント
Beder
Beder 2017 年 1 月 9 日
Not sure about it - without trying - maybe you need so replace sum(A{k,C}) by sum(A(k,C))?
John
John 2017 年 1 月 9 日
thanks, @Peter Albrecht. I tried this one, but the following error occurs: Undefined function 'sum' for input arguments of type 'table'.

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

回答 (1 件)

Guillaume
Guillaume 2017 年 1 月 9 日
編集済み: Guillaume 2017 年 1 月 9 日
Clearly from the error message, one of the rows of A has a double matrix instead of a cell array in column L. The simplest way to find out:
dbstop if error
run your code and look at the value of k when it breaks into the debugger. This is the offending row.
edit: the following will give you all the offending rows:
find(cellfun(@(r) ~iscell(r), A.L))

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by