'Conversion to double from cell is not possible.'

14 views (last 30 days)
Problem: I have this code that calculate postfix but when I use it gives me an error.
Postfix(nCols)=struct('matches',[],'postfix',[]);
for k=1:nCols
for l=1:25
% Calcolo postfissi associati alle traiettorie dei singoli utenti
if ~isempty(semanticTrajCompact(1,k).TrajCompact)
[Postfix(1,k).matches(:,l), Postfix(1,k).postfix(:,l)] = regexp(semanticTrajCompact(1,k).TrajCompact,sprintf('%d%d(.*)',digits{1}(l),digits{2}(l)),'match','once','tokens');
end
end
end
The error is:
'Conversion to double from cell is not possible.'
Can you give me some suggestions to solve it?
  4 Comments
Walter Roberson
Walter Roberson on 17 Dec 2015
Try
Postfix(nCols) = struct('matches',[],'postfix',{{}});
Stephen23
Stephen23 on 17 Dec 2015
Edited: Stephen23 on 17 Dec 2015
@Walter Roberson: this will never work. From struct help: "If any value input is an empty cell array, {}, then output s is an empty (0-by-0) structure", which you then try to allocate to a single element. Ouch!

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 17 Dec 2015
Edited: Stephen23 on 17 Dec 2015
Try
tmp = repmat({{}},1,nCols);
Postfix = struct('matches',tmp,'postfix',tmp)
Your original problem is that you defined the fields values to be (empty) doubles (using 'matches',[]), and you then try to extend these doubles with cell arrays. So this is the error you are making:
>> X = [];
>> X(:,1) = {'a','b','c'}
The following error occurred converting from cell to double:
Error using double
Conversion to double from cell is not possible.
The solution is to define the field values to be empty cell arrays, which can then be extended with the output of regexp. To do this we need to read the struct documentation carefully, because the highest level of cell array is not considered to be data, but determines the size of the structure. So you need to wrap the empty cell arrays in a cell array of the size of structure that you need. This is what my code does.
This is a good lesson in actually reading the documentation, and actually checking that an operator really does what you think it does. Sadly beginners seem to think that these are both a waste of time.

More Answers (0)

Categories

Find more on Data Type Conversion 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!