'Conversion to double from cell is not possible.'
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ely may
am 16 Dez. 2015
Bearbeitet: Stephen23
am 17 Dez. 2015
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 Kommentare
Walter Roberson
am 17 Dez. 2015
Try
Postfix(nCols) = struct('matches',[],'postfix',{{}});
Akzeptierte Antwort
Stephen23
am 17 Dez. 2015
Bearbeitet: Stephen23
am 17 Dez. 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.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!