How to delete rows in a table
Ältere Kommentare anzeigen
Hello, i have to analyse some data of sensors at different dates. I want to import the data and remove the entire row when there is a data missing = cell that contain 0 (and later NaN). After import my table looks like:
Date Info1 info2 info3... info26
NaN 30 NaN 40 ... NaN
734567 0 40 40 ... 40
785365 0 0 40 ... 0
789456 40 40 0 ... 40
789456 40 40 0 ... 40
785412 40 40 40... 40
778858 40 40 40 ... 40
In this case only the two last rows should be kept. I need the data of info1,2,3 to stay at the same date when i have all the data. When i import the data the table is in dataset, so i tried converting it to double, but it doesn't work.
This is my code:
%if true
T = importfile8('file.csv', 1, 16786);
T2=double(T(:,1:25));
i=1;
col=1;
for i=1:16782
for col=1:25
if T2(i,col)==0
T2(i,:)=[];
else col=col+1;
end
end
i=i+1;
end
There are 16786 rows and 25 columns and I get this error message: "Index exceeds matrix dimensions.
Error in ValuesFilter (line 11) if T2(i,col)==0" I just want to specify that i work with the R2013a version of matlab too. Thank you!
1 Kommentar
KL
am 29 Mär. 2017
Please format the question in a better way, this is clearly not readable.
Akzeptierte Antwort
Weitere Antworten (1)
Peter Perkins
am 31 Mär. 2017
Bearbeitet: Peter Perkins
am 31 Mär. 2017
It's hard to tell what you actually have. You say both "table" and "dataset". The latter is a class in the Statistics Toolbox, the former is a newer class in core MATLAB that, unless you are using a pre-R2013b version of MATLAB, you probably want to be using instead, by calling readtable on your CSV file.
The first column of numbers look like datenums. Unless you're using pre-R2014b, you probably want to use datetimes. Given the timestamps, if you have R2016b or later, you probably want to look at timetables. And contrary to what you've said, it looks like you have 27 columns of numbers, one of datenums and 26 of other values.
If this is really a table, and you are using R2016b or later, use rmmissing (probably preceeded by standardizeMissing to deal with the "zeros as missing"). If not, then something similar to what Rik suggested, tailored to a table, does what you want:
i = any(t{:,:} == 0,2) | any(isnan(t{:,:},2);
t(i,:) = [];
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!