Adding/removing commas
17 次查看(过去 30 天)
显示 更早的评论
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 个评论
David Hill
2020-6-4
Cannot understand what you are asking for. Providing a sample or copy of ICD and expected output would help.
回答(1 个)
Image Analyst
2020-6-4
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

