filtering out negative values and zeroes from .txt data file
조회 수: 19 (최근 30일)
이전 댓글 표시
I have some .txt files with me and there are zeros and negtive values in some columns I would like to filter out the data like just exclude the zeros and negative values and get the data in the workspace. How should I do that. The txt file is attached below. Thanks in advance for your help .
채택된 답변
Walter Roberson
2020년 8월 18일
varmask = varfun(@isnumeric, YourTable, 'output', 'uniform');
as_numeric = YourTable{:,varmask};
good_data_mask = all(as_numeric > 0, 2);
reduced_table = YourTable(good_data_mask, :);
This removes an entire row if any non-positive value is found anywhere in the row.
댓글 수: 3
Walter Roberson
2020년 8월 18일
What would you mean by "remove" in that case? Do you want the row removed? Do you want the column removed? Do you want the value replaced by nan? Do you want a space to replace the entry?
If you want a space to replace the entry it will be necessary to convert the column to a cell array of character vectors and put the empty character vector in that location. MATLAB does not have any way to mark an element in a numeric array as being "missing" in a sense that the output for the location is to be emptiness.
추가 답변 (1개)
KSSV
2020년 8월 17일
Let A be an array..you can remove the negative values using:
A(A<0) = [] ; % this remove all negative values
A(A<=0) = [] ; % this will remove negative values and zeros
A = A(A>0) ; % This will pick all positive values
s = sign(A) ; % this will sign of values 1 for positive and -1 for negative
댓글 수: 5
Walter Roberson
2020년 8월 18일
first_column_data = x130_Gmod12_SS.Data{:,1};
WIth the () brackets you get a sub-table, not the contents of the variable.
However this does not explain why x130_Gmod12_SS does not permit dot indexing. What is class(x130_Gmod12_SS) ?
eval(sprintf('%s = data.%s', x{1}, x{1}));
is it possible to remove all the zeroes and negative values all at once?.
You have some columns that are non-numeric. Should only the numeric columns be processed?
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!