Adding/removing commas

조회 수: 14 (최근 30일)
Deedee
Deedee 2020년 6월 4일
댓글: Deedee 2020년 6월 5일
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일
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
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.

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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by