Converting cells to strings
3 次查看(过去 30 天)
显示 更早的评论
Actually I have a 1x1 struct that I am extracting data from.
But the data is a mixture of numbers and strings.
I want to convert all of the data into strings so that I could store it in a separate cell array.
'char' doesn't do a good conversion, because it truncates everything to 255.
0 个评论
采纳的回答
Andrei Bobrov
2011-4-14
variant
dc = struct2cell(data);
t1 = cell2mat(cellfun(@(x)[ischar(x) isnumeric(x)],dc,'UniformOutput', false));
celstr = dc(t1(:,1));
num = dc(t1(:,2));
% if there is a field with a cell with characters and numbers
t2 = ~any(t1,2);
dcc = cell(cellfun(@(x)x(:)',dc(t2),'UniformOutput', false));
dcc2 = [dcc{:}];
t3 = cell2mat(cellfun(@(x)[ischar(x) isnumeric(x)],dcc2,'UniformOutput', false)')';
celstr = [celstr;dcc2(t3(1,:))'];
num = [num;dcc2(t3(2,:))'];
0 个评论
更多回答(3 个)
Jan
2011-4-15
Are the numbers scalars or arrays? Which format do you want as output?
C = struct2cell(DataStruct);
numIndex = find(not(cellfun('isclass', C, 'char')));
for i = reshape(numIndex, 1, [])
C{i} = numstr(C{i});
end
0 个评论
Walter Roberson
2011-4-15
Which MATLAB version are you using? char() has accepted up to 65535 since approximately MATLAB 6.2.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!