filtering out negative values and zeroes from .txt data file

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 .

 Accepted Answer

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 Comments

is it possible to remove only the particular non zero positive values? the table is 1440*75
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.
like value to be replaced by nan

Sign in to comment.

More Answers (1)

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 Comments

data=(readtable('[208] 2017-07-01.txt'));
for i=1:width(data)
x = data.Properties.VariableNames(i);
eval(sprintf('%s = data.%s', x{1}, x{1}));
end
A = data;
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
I tried the above code and i get the error
Operator '<' is not supported for operands of type 'table'.
so i tried converting the table to array using table2array but then i get this error
Error using table2array (line 37)
Unable to concatenate the specified table variables.
Caused by:
Error using datetime/horzcat (line 1341)
All inputs must be datetimes or date/time character vectors or date/time strings.
In here A is a table.....I mentioned that A should be an array i.e a row matrix or a column matrix. Try picking any column of table.. A.(1) or A.(2) etc.
first_column_data = x130_Gmod12_SS.Data(:,1);
i tried adding the first column and i get the error
dot indexing is not supported for this variable
how do i keep this as a table and change the values?
the data in the work space is 1440*75 (table), is it possible to remove all the zeroes and negative values all at once?.
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?

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!