MATLAB Answers

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

2 views (last 30 days)
Forrest Ward
Forrest Ward on 24 Jun 2020
Answered: Eric Sofen on 25 Jun 2020
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

dpb
dpb on 24 Jun 2020
Think be useful to see the text file, yes. Don't fully understand w/o the reason for the message--doesn't seem as though would make a lot of sense to try to average nonnumeric data and don't follow what is in the timetable that doesn't belong -- or why it would be there to start with?
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.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by