Adding/removing commas
18 views (last 30 days)
Show older comments
If I try this, then I get a list of numbers with no delimitation
Data = table2array(ICD)
for num=2:1
Data(num,18)=strcat(Data(num,9),Data(num,10),Data(num,11),Data(num,12));
e.g. cell 1: 35719
cell 2: blank
cell 3: 2
If I try this, then a comma is inserted after each number but it is also inserted if there is no number
Data(num,18)=strcat(Data{num,9},{','},...
Data{num,10},{','},...
Data{num,11},{','},...
Data{num,12})
e.g. cell 1: 3,5,7,19
cell 2: ,,,
cell 3: 2,,,
What I am looking for is to insert comma only if there is a number in the cell array and also no trailling commas
e.g. cell 1: 3,5,7,19
cell 2: blank
cell 3: 2
Thank you!
2 Comments
David Hill
on 4 Jun 2020
Cannot understand what you are asking for. Providing a sample or copy of ICD and expected output would help.
Answers (1)
Image Analyst
on 4 Jun 2020
So evidently your cell array is called "Data" (because you use braces with it) but you got Data from the table2array function so it's not a cell array, it's a double array. Please explain the contradiction.
Why not just use the ICD table itself directly? And you want to take the contents of each cell (or table entry), and, if it's numerical, convert it to a string. And if the numerical contents are a vector (more than one number), make the string have commas between the numbers, right?
Is this what you want:
% Make cell array
for row = 1 : 5
numElements = randi(5, 1);
ca{row} = randi(9, 1, numElements);
end
celldisp(ca)
% Now turn cell array contents into strings with commas between numbers
for row = 1 : length(ca)
thisCellsContents = ca{row};
if isnumeric(thisCellsContents)
str = sprintf('%.1f, ', thisCellsContents)
% Get rid of trailing comma and space.
ca{row} = str(1:end-2);
end
end
celldisp(ca)
3 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!