Trasform to cell of strings

1 view (last 30 days)
ely may
ely may on 3 Dec 2015
Edited: Stephen23 on 3 Dec 2015
Problem: I have postfix (attached) (137x25) cell that contain cell of char (Ex postfix(6,8)=postfix{6,8}{1,1}<1x3char>). I want to trasform it in (137x25) cell of char.
Postfix is created in this way:
for l=1:25
[matches(:,l), postfix(:,l)] = regexp(semanticTrajCompact(1,4).TrajCompact,sprintf('%d%d(.*)',digits{1}(l),digits{2}(l)),'match','once','tokens');
end
I have tried different solutions:
Solution 1
numIndex = cellfun('isclass', postfix, 'double');
tmpStr = sprintf('%g;', postfix{numIndex});
postfix(numIndex) = dataread('string', tmpStr, '%s', 'delimiter', ';');
Solution 2
postfix(cellfun(@isempty,postfix))={''};
Solution 3
postfix(cellfun(@isnumeric, postfix)) = cellfun(@(x) sprintf('%.5f', x), postfix(cellfun(@isnumeric, postfix)), 'UniformOutput', false)
Solution 4
I try to use
char(postix)
Anyone of this solution trasform postfix in a (137x25 array of char). Can you give me other ideas?

Accepted Answer

Stephen23
Stephen23 on 3 Dec 2015
Edited: Stephen23 on 3 Dec 2015
I already answered this in your earlier question. Your claimed that "I have tried your code but it continues to give me an error", but it turns out you did not even try it.
Your cell array contains multiple empty cells, and also cells that contain cells with non-scalar strings. In the general case it is not possible to convert a cell array with non-scalar string into a character array of the same size as the cell array. The only case where this conversion is guaranteed is if every string is scalar, which is not the case for your cell array.
>> postfix(cellfun(@isempty,postfix)) = {''};
>> out = reshape([postfix{:}],size(postfix));
>> size(out)
ans =
137 25
>> iscellstr(out)
ans = 1
Consider the example from my original answer: if you converted Y (a cell-array of strings) to a character array, what size would you expect it to be? Your requirement is that it should be the same size as X, but this is not possible:
>> X = {{'A'},{'bb'};{'CCC'},{'d'}}
X =
{1x1 cell} {1x1 cell}
{1x1 cell} {1x1 cell}
>> Y = reshape([X{:}],size(X))
Y =
'A' 'bb'
'CCC' 'd'
>> char(Y)
ans =
A
CCC
bb
d

More Answers (0)

Categories

Find more on Characters and Strings 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!