MATLAB Answers

How to readtable filtering columns and rows?

89 views (last 30 days)
Mantas Vaitonis
Mantas Vaitonis on 16 Nov 2018
Commented: Jan on 6 Dec 2020
Hello,
Maybe someone knows if there is a way to readtable not only defining the column names but defining rows values as well? I have a *.csv file :
ReceivingDate ReceivingTime SecurityID Asset EntryType EntryPrice EntrySize
20180429 16:53:28.944250499 10062 FOB A 0 0
20180429 16:53:28.944250499 10062 FOB B 0.03125 5
20180429 16:53:33.913990105 10062 FOB A 0 0
20180429 16:53:33.913990105 10062 FOB B 0 0
20180429 16:53:41.173484956 10062 FOB A 0.03125 5
20180429 16:53:41.173484956 10062 FOB B 0 0
20180430 16:54:55.254652081 10062 FOB A 0.03125 5
20180430 16:54:55.254652081 10062 FOB B 0.078125 1
20180430 17:06:59.219609240 10062 FOB A 0.03125 5
20180430 17:06:59.219609240 10062 FOB B 0 1
20180430 17:11:55.316770824 10062 FOB A 0.03125 5
20180430 17:11:55.316770824 10062 FOB B 0.0078125 1
I use this code to define which columns to read:
opts = detectImportOptions('trys.csv');
opts.SelectedVariableNames = {'ReceivingDate','ReceivingTime','EntryType','EntryPrice'};
t = readtable('trys.csv',opts);
And the result is as follows:
ReceivingDate ReceivingTime EntryType EntryPrice
20180429 16:53:28.944250499 A 0
20180429 16:53:28.944250499 B 0.03125
20180429 16:53:33.913990105 A 0
20180429 16:53:33.913990105 B 0
20180429 16:53:41.173484956 A 0.03125
20180429 16:53:41.173484956 B 0
20180430 16:54:55.254652081 A 0.03125
20180430 16:54:55.254652081 B 0.078125
20180430 17:06:59.219609240 A 0.03125
20180430 17:06:59.219609240 B 0
20180430 17:11:55.316770824 A 0.03125
20180430 17:11:55.316770824 B 0.0078125
Would it be possible to filter row values (20180430), that final readtable result would be
ReceivingDate ReceivingTime EntryType EntryPrice
20180430 16:54:55.254652081 A 0.03125
20180430 16:54:55.254652081 B 0.078125
20180430 17:06:59.219609240 A 0.03125
20180430 17:06:59.219609240 B 0
20180430 17:11:55.316770824 A 0.03125
20180430 17:11:55.316770824 B 0.0078125

  0 Comments

Sign in to comment.

Accepted Answer

Jan
Jan on 16 Nov 2018
Edited: Jan on 16 Nov 2018
There would not be a big advantage, if readtable filters the contents of the data already, because it has to read the complete file in any way. I'd prefer to let readtable read the complete file and to select the wanted data afterwards. Otherwise readtable becomes a multi-purpose function, a "one hammer hits all I need".

  2 Comments

Jorge Filipe
Jorge Filipe on 6 Dec 2020
When you have an huge csv of many GB, that is not an option.
Jan
Jan on 6 Dec 2020
A CSV file of many GB is a design error already. Human readable files are useful only, if they have to be read end edited by human. A "many GB" file is too large to do this. So a binary file is the better choice.

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