Adding/removing commas

18 views (last 30 days)
Deedee
Deedee on 4 Jun 2020
Commented: Deedee on 5 Jun 2020
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
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.
Deedee
Deedee on 4 Jun 2020
ICD = readtable(files_in,'ReadVariableNames',false)
The code is reading an Excel file. After matching cells in a row with cells in the first column, the code outputs the corresponding row numbers in separate cell columns. Then I am trying to concatenate those values (corresponsing row numbers) in one cell and separate them by commas. The issue I am running into is that some cells are empty. I hope that makes a little more sense.

Sign in to comment.

Answers (1)

Image Analyst
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
Image Analyst
Image Analyst on 5 Jun 2020
I'll just wait for you to upload the workbook.
Deedee
Deedee on 5 Jun 2020
This is a small part of the workbook. In the "This is what I am looking for" column I manually added what I hope Matlab can do.
I want to import this kind of file and Matlab to export the Excel file with "This is what I am looking for" column included.

Sign in to comment.

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!