How can I split comma-separated numbers inside a cell of a cell array?

조회 수: 37 (최근 30일)
Alessandro Dulja
Alessandro Dulja 2016년 10월 15일
댓글: Walter Roberson 2016년 10월 16일
Hi everyone, I have a cell array 200x1 and in each cell I have two or more comma-separated numbers. I would like to create an array in which I have, in each row, one number per cell. For example, if cell {3,1} is 23, 45, 67 I want to create an array in which cell {3,1} contains number 23, cell {3,2} contains number 45 and cell {3,3} contains number 67. Thanks in advance
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 10월 15일
Is your input a cell array of strings?
Is your output to be a cell array of split strings, or is your output to be a cell array of numbers of the converted strings?
Alessandro Dulja
Alessandro Dulja 2016년 10월 16일
편집: Alessandro Dulja 2016년 10월 16일
Yes the input is a cell array of strings, while I want it to become a cell array of numbers.

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

답변 (2개)

goc3
goc3 2016년 10월 15일
Let me know if this is what you're looking for:
% define comma-delimited data set
cell_dat = {'13,15,21'; '42,40,47,11,30'; '15,51,23'; '67,76'}
% split comma-delimited sets and convert to number format
cell_dat_split = cellfun(@(x)regexp(x,',','split'),cell_dat,'UniformOutput',0)
  댓글 수: 2
Alessandro Dulja
Alessandro Dulja 2016년 10월 16일
Using your code I get a cell array in which in each cell there is another cell array. The cell array inside the cell array is what I want. For example, with your code I get a cell array in which the first cell contains a cell array 1x3. The cell array 1x3 is what I want, but I don't want it to be contained in a cell of another cell array.
Walter Roberson
Walter Roberson 2016년 10월 16일
It has to be in a cell because you have a different number entries for each.
Glen's version is splitting the strings but not converting to numeric. My version converts to numeric vectors

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


Walter Roberson
Walter Roberson 2016년 10월 15일
cell_dat = {'13,15,21'; '42,40,47,11,30'; '15,51,23'; '67,76'};
cell_dat_split = cellfun(@(S) sscanf(S, '%f,').', cell_dat, 'Uniform', 0);

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by