필터 지우기
필터 지우기

Delete values in a table but keep the cells

조회 수: 4 (최근 30일)
Daniel Garratt
Daniel Garratt 2021년 3월 1일
댓글: Jorg Woehl 2021년 3월 2일
I have a 1846x72 table that contains strings (I assume they are strings since the value is in '') and numbers. The first two columns are either NaN or a number. I want to go through each row and, if the second column in that row is not NaN, make the following columns all NaN (or empty) for only that row.
This is the code I a have:
for r = 1:nRows
if isnumeric(df{r,2})
for c = 3:nCols
df{r,c} = NaN;
end
end
end
This is the error I am receiving:
Conversion to cell from double is not possible.
I can not include the dataset as it pertains to my work and I am not allowed.
  댓글 수: 3
Simon Allosserie
Simon Allosserie 2021년 3월 2일
Use df{r,c} = [], that should work
Daniel Garratt
Daniel Garratt 2021년 3월 2일
That returns the error:
"The value on the right-hand side of the assignment has the wrong width. The assignment requires a value whose width is 1."

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

채택된 답변

Jorg Woehl
Jorg Woehl 2021년 3월 2일
편집: Jorg Woehl 2021년 3월 2일
Try
df{r,c} = {''}
instead. You should be able to avoid the inner loop entirely by using
df{r,3:nCols} = {''}
Also, to check if the second column in row r is not NaN, use
if ~isnan(df{r,2})
...
end
isnumeric(NaN) will return logical true, as NaNs belong to the floating point class.
  댓글 수: 3
Daniel Garratt
Daniel Garratt 2021년 3월 2일
It took a little changing but this is what I have working.
% import data
df = readtable('path to file');
cellarray = table2cell(df);
[nRows, nCols] = size(df);
% go through each row and empty all remaining cells of each row after the
% second column if the second column is not equal to NAN
for r = 1:nRows
if ~isnan(cellarray{r,2})
for c = 3:nCols
cellarray{r,c} = {''};
end
end
end
Thanks for your help.
Jorg Woehl
Jorg Woehl 2021년 3월 2일
I'm glad you were able to make it work! Not sure why you've still received a type conversion error when working with the table... probably something specific to the imported table.
If you can provide a small table with (made-up) sample data, we might be able to figure out what is going on. But the main thing is that your code is running.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by