MATLAB Answers

Invalid data type - converting to cell array to run functions

1 view (last 30 days)
I essentially want to pull the data from timepoints of interest and run the same functions for each "bout of data" even though these bouts are not always the same length. I understand a cell array will be helpful for this, but when I try to write for loop, the data is no longer in 'double' form to perform functions.
How can I convert to cell array but keep the data in 'double' type?
Example failure 1
% tot_dff: vector of data across time
%f_i & f_o : variables corresponding to time on and off for periods of interest in the data
for i=1:length(f_i);
dff{i} = num2cell(tot_dff(1,f_i(i):f_o(i)));
X = cellfun(@mean,dff);
Y = cellfun(@max,dff);
Error using sum
Invalid data type. First argument must be numeric or logical.
% In this case dff is 1x46 cell which each cell containing cells corresponding to each data point.
Example failure 2
for i =1:length(f_i);
dff{i} = {tot_dff(1,f_i(i):f_o(i))};
%in this case, dff is 1x46 cell array with each cell containing my list of numbers in [ ] . Still can't run the functions on it and get the same error.
When I create my cell array by hand
dff = {tot_dff(1:3),tot_dff(6:10)} ........ this works but will take me forever!
Thank you for your help!


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 21 Oct 2020
Edited: Stephen Cobeldick on 21 Oct 2020
In both cases your code nests a cell array inside a cell array:
dff{i} = num2cell(tot_dff(1,f_i(i):f_o(i)));
% ^^^^^^^^^ ^ % define the RHS as a cell array
% ^ ^ % place the RHS in another cell array
The nesting is superfluous, you only need one cell array (on the LHS), e.g.:
nml = length(f_i);
dff = cell(1,nml); % preallocate!
for k = 1:nml;
dff{k} = tot_dff(1,f_i(k):f_o(k));
end % ^ ^ do NOT make this a cell array !

More Answers (0)

Community Treasure Hunt

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

Start Hunting!