Using "find" in cell inside cell with different data types
Show older comments
Hello,
my data is 1x253 cell, inside each are 253 further cell matrices with different sizes. An example of one of the cells is:
'COP PE Equity' NaN NaN 'COR PE Equity' NaN NaN NaN NaN
735452 6332 'NoValue' 633 '#N/A Invalid Security' NaN 'NoValue'0 NaN
735475 2232 'NoValue' 223 NaN NaN NaN NaN NaN
735545 304.1 'NoValue' 304.1 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
I would like to implement the following:
for i=1:4:3497
if strcmp(data{1,a}{2,i},'#N/A Invalid Security') ==0 && strcmp(data{1,a}{2,i},'#N/A N/A') ==0 && strcmp(data{1,a}{2,i},'Date') ==0 && strcmp(data{1,a}{2,i},'TURNOVER') ==0
for j=1:size(date,1)
index = find(date(j,1)==data{1,a}(:,i));
if isempty(index)
final{1,a}(j,ceil(i/4)) = nan;
else
final{1,a}(j,ceil(i/4)) = data{1,a}{index,i+4};
end
end
end
end
So, I would like to go in each cell matrix inside cell "data", search for each element from "date" vector, and use the fourth column value for the orresponding row (index) in a new cell "final".
When I run this code, I get error message
"Function 'subsindex' is not defined for values of class 'cell'.".
When I try to use:
find(date(j,1)==cell2mat(data{a}(:,i))),
I get error
"Error using cell2mat (line 45) All contents of the input cell array must be of the same data type."
Could you please advise me on how to proceed?
Thanks a lot.
6 Comments
Walter Roberson
on 19 Sep 2017
Note: the expression
if strcmp(data{1,a}{2,i},'#N/A Invalid Security') ==0 && strcmp(data{1,a}{2,i},'#N/A N/A') ==0 && strcmp(data{1,a}{2,i},'Date') ==0 && strcmp(data{1,a}{2,i},'TURNOVER') ==0
can be simplified to
if ~ismember(data{1,a}{2,i}, {'#N/A Invalid Security', '#N/A N/A', 'Date', 'TURNOVER'})
but you would probably be better using
if ischar(data{1,a}{2,i}) && ~ismember(data{1,a}{2,i}, {'#N/A Invalid Security', '#N/A N/A', 'Date', 'TURNOVER'})
Ekaterina Serikova
on 20 Sep 2017
Guillaume
on 20 Sep 2017
In my opinion, you would be better off changing the way you imported that data. This would make your current task and any other task where you have to search particular values much easier (even possibly trivial).
You seem to have headers mixed with data. The headers should be separated (in another cell array, or even better using tables which are much easier to use than cell arrays)
You have a mix of char arrays and numbers in the same column. What is the difference between NaN and 'NoValue'. Probably none, so these 'NoValue' could be converted to NaN.
Once the type of each column is clearly defined, it would be much easier to search them. Using table(s) instead of cell arrays would make it even easier.
Ekaterina Serikova
on 20 Sep 2017
Walter Roberson
on 20 Sep 2017
You can create cell arrays of tables.
Guillaume
on 20 Sep 2017
... or you could have one table where each column is a cell array of column, depending on what's better for later processing.
A cell array of tables would be easier to generate but possibly harder to search.
Alternatively, you could have just one big table with an additional column indicating which file/cell array the data came from. This would make searching/filtering almost trivial.
Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!