changing the cell values into normal columns

6 ビュー (過去 30 日間)
Mohamuud hassan
Mohamuud hassan 2015 年 5 月 15 日
編集済み: Stephen23 2015 年 5 月 15 日
hello all, i have string data which contains 3groups in one cell how i can change into normal array i mean each character with one column. for instance
Data={'020';'1014';,000'};
how i can change those into each value in one column except 1014 which i want to store as 1 for one column and 0 for one column but 14 in one column as pair.
help me for solving this problem.
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 5 月 15 日
Can you post the expected result?
Mohamuud hassan
Mohamuud hassan 2015 年 5 月 15 日
編集済み: Mohamuud hassan 2015 年 5 月 15 日
thank you Abdelmalik. the expected result will be
data=[0;2;0;1;0;14;0;0;0];
thank you

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 5 月 15 日
編集済み: Stephen23 2015 年 5 月 15 日
A simple one-line solution using sscanf inside of cellfun and cell2mat:
>> Data={'020';'1014';'000'};
>> cell2mat(cellfun(@(s)sscanf(s,'%1f%1f%f'),Data, 'UniformOutput',false))
ans =
0
2
0
1
0
14
0
0
0
Note the format string used in sscanf: the %1f token parses just one character, thus allowing the automatic parsing of the first two characters individually.

その他の回答 (2 件)

Guillaume
Guillaume 2015 年 5 月 15 日
One possible way would be to use a regular expression. Assuming that any overspill digit ends up in the third column:
Data={'020';'1014';'000'};
datasplit = regexp(Data, '(\d)(\d)(\d+)', 'tokens', 'once'); %split into three tokens, the first two just one digit.
datasplit = vertcat(datasplit{:}); %merge all rows into one cell array
newdata = cellfun(@str2double, datasplit) %and convert to numeric

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 5 月 15 日
Data={'020';'1014';,'000'}
idx=~strcmp(Data,'1014')
ix=~idx
out=cell(numel(Data),3)
out(idx,1)=cellfun(@(x) x(1),Data(idx),'un',0)
out(idx,2)=cellfun(@(x) x(2),Data(idx),'un',0)
out(idx,3)=cellfun(@(x) x(3),Data(idx),'un',0)
out(ix,1)=cellfun(@(x) x(1),Data(ix),'un',0)
out(ix,2)=cellfun(@(x) x(2),Data(ix),'un',0)
out(ix,3)=cellfun(@(x) x(3:4),Data(ix),'un',0)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by