How to import data from attatched csv file?
Show older comments
In the attatched csv file there is a lot of clutter other than data, first 11 lines are not needed, so I used
T=readtable('data.csv','HeaderLines',11);
but in the data also, every data point has its unit GHz attached to it, which I dont want to import into my array, how can I stop that GHz from being imported. What more lines of code needs to be added. For ex: output from above readtable function is :
Var1 Var2
_____________ ______
{'1.000 GHz'} 200
{'1.100 GHz'} 200
{'1.200 GHz'} 200
{'1.300 GHz'} 200
{'1.400 GHz'} 200
{'1.500 GHz'} 200
{'1.600 GHz'} 199.99
{'1.700 GHz'} 199.99
{'1.800 GHz'} 199.99
{'1.900 GHz'} 199.99
{'2.000 GHz'} 199.99
It is taking var1 as string because of the presence of GHz, please someone help me remove that. Also explain how can I name change names of these var1 and var2 by using appropritae argument is readtable function.
I noticed another problem, in the var2 column the data is till 3 decimal places, but imported data is automatically rounded off to 2 decimal place, i don't want it to be rounded off. How to avoid that?
Thankyou in advance.
2 Comments
Cris LaPierre
on 12 Aug 2020
There's no attached csv file.
Prabhanshu Chandra
on 12 Aug 2020
Accepted Answer
More Answers (1)
Jeremy Hughes
on 12 Aug 2020
In R2020a,
T = readtable('data.csv','TrimNonNumeric',true,'NumHeaderLines',11);
or in earlier releases,
opts = detectImportOptions('data.csv','TrimNonNumeric',true,'NumHeaderLines',11);
T = readtable('data.csv',opts);
5 Comments
Cris LaPierre
on 12 Aug 2020
I did not know about 'TrimNonNumeric'. Thanks for sharing!
Prabhanshu Chandra
on 12 Aug 2020
Edited: Prabhanshu Chandra
on 12 Aug 2020
Jeremy Hughes
on 12 Aug 2020
Hmm, the NumHeaderLines thing seems like a bug. Reported.
Prabhanshu Chandra
on 12 Aug 2020
Jeremy Hughes
on 12 Aug 2020
Oh, I see, never mind. Not a bug exactly--just a quirk of CSV data. The first row begins with a double quote character.
CSV spec says that should be one field, containing the newlines and all... so there is only one line being interpreted by the parser. The first "row" considering quotes looks like this
"freq\nReferences : ['R']\nDependency : [freq]\nNum. Points : [91]\nMatrix Size : scalar\nType : Real", ...
"R\nReferences : []\nDependency : [freq]\nNum. Points : [91]\nMatrix Size : scalar\nType : Real"
If you leave off the NumHeaderLines it treats this as a row, and stripes away everything but the 91. and I see:
T = readtable('data.csv','TrimNonNumeric',true); head(T)
ans =
8×2 table
Var1 Var2
____ ______
91 91
1 200
1.1 200
1.2 200
1.3 200
1.4 200
1.5 200
1.6 199.99
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!