I want to import only specific Types of variables using readtable.

10 views (last 30 days)
Hello, so I am using 'readtable' to import some text data that I have. I am then using 'table2timetable' to be able to use my data as a time table. The data if have is collected sparatically at different minutes and hours, so I am using
NewData = retime(TimeTableOut,'hourly','mean');
to average the values into a whole hour row.
But I am getting this error.
All variables in input timetables must be numeric, datetime, or duration when synchronizing using 'mean'.
I understood what that meant so I deleted the coumns (variables) of the imported table that contain data that is not of these types. It worked once I did this, but I would like to know if there is a better way to do this so I don't have to go in and delete columns myself every time. I would like to import data columns (variables) that contain only these three types of data. Any help is appreciated! If you need, I can attach my table or text file that I am importing.
  3 Comments
Forrest Ward
Forrest Ward on 24 Jun 2020
I'm attaching the text file. You can see from it that there are some variables that contains 'char' and non-numeric stuff. I am not trying to average non-numeric data which is why I would like to delete those columns or never import them in the first place.
dpb
dpb on 24 Jun 2020
Presuming these files all follow same format, I'd suggest building an importoptions object from the output of detectImportOptions that contains the selected input variables you want and use it when reading the files. You could possibly have more than one if there are different subsets of variables wanted for particular analyses.
This can be done once and saved in a .mat file and loaded and used for any file of the same structure.
Alternatively, you can just accept what readtable finds and then use isnumeric() on the columns to get rid of the unwanted columns after the fact.

Sign in to comment.

Accepted Answer

Eric Sofen
Eric Sofen on 25 Jun 2020
You can use vartype to subscript into the table and select only variables that are a certain datatype (or meta-type like 'numeric'). Then, if you want the other data as well, you might circle back with ~vartype('numeric') and use one of the other retime aggregation methods (e.g. 'firstvalue') that will work with text data.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!