Converting cells to strings

조회 수: 5 (최근 30일)
John Brathwaite
John Brathwaite 2011년 4월 14일
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.

채택된 답변

Andrei Bobrov
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,:))'];

추가 답변 (3개)

Jan
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

Walter Roberson
Walter Roberson 2011년 4월 15일
Which MATLAB version are you using? char() has accepted up to 65535 since approximately MATLAB 6.2.
  댓글 수: 1
Jan
Jan 2011년 4월 15일
Even MATLAB 5.3 had 16 bit CHARs already.

댓글을 달려면 로그인하십시오.


John Brathwaite
John Brathwaite 2011년 4월 18일
This is interesting the issue with char. I am using matlab 7.8.0
But I was able to get in done with "struct2cell".
Thanks for your help!

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by