Adding/removing commas

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
David Hill 2020 年 6 月 4 日
Cannot understand what you are asking for. Providing a sample or copy of ICD and expected output would help.
Deedee
Deedee 2020 年 6 月 4 日
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.

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

回答 (1 件)

Image Analyst
Image Analyst 2020 年 6 月 4 日

0 投票

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 件のコメント

Deedee
Deedee 2020 年 6 月 4 日
It's a double array - mistyped.
I am looking for an Excel Output file with a column that lists the row numbers that match the values in columns 9 through 17.
I have most of the code working. The only thing I can't figure out is the commas. I am trying to take the contents of columns 9 through 17 and check if they have a match in the first column. If there is a match, the code outputs the row number that matches as shown in the picture in the earlier comments.
The catch is that some cells in columns 9 through 17 are empty and if I use this
Data(num,18)=strcat(Data{num,9},{','},...
Data{num,10},{','},...
Data{num,11},{','},...
Data{num,12})
then I get all those extra commas.
(the code example I used only goes through column 12 for time sake)
Image Analyst
Image Analyst 2020 年 6 月 5 日
I'll just wait for you to upload the workbook.
Deedee
Deedee 2020 年 6 月 5 日
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.

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

製品

タグ

質問済み:

2020 年 6 月 4 日

コメント済み:

2020 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by