Stroring data from text file into cell array
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone,
Apologies for the stupid question, I am new to extracting data from txt files. Here is the problem, I have a lot of data stored in a txt file (in total
rows) which is of the following form:
802|201308|9|204307|40140|000|1|I|107|999|154000|107|4.125|R|N|FRM|CA|SF|92400|F113Q|N|360|01|Norges, N.A.|Norges, N.A.||F108X|
My attempt so far has been:
fileID = fopen('thetextfile.txt');
formatSpec = '%s';
N = 27;
C_text = textscan(fileID,formatSpec,N,'Delimiter','|');
fclose(fileID);
I am happy how the data has been stored, the only problem is that just the first row of the text file has been saved in C_text. In other words, C_text is a 27×1 cell array. The goal would be to have a 27x>2000000 celly array, which I can then manipulte further. I am not sure whehter this is feasible given the size of the txt file. If there is a better and more efficient way to do it then please let me know. Any help is appreciated.
Many thanks!
Robert
0 commentaires
Réponse acceptée
Cris LaPierre
le 12 Nov 2020
I would suggest using readtable. It makes your data much more accessible. If there is a header row, it will use those names as the variable names (each column in a table is a variable). Without them, it assigns names (see below). You can then access your data using dot notation (table.variable).
Here's an example using the sample data you provided. It should be able to scale to handle your complete data set.
data=readtable("thetextfile.txt","Delimiter","|")
data.Var3
3 commentaires
Cris LaPierre
le 13 Nov 2020
It does try to do a lot of autodetecting under the hood, so sometimes performance can be improved by setting more of the import settings (data type, format, etc.) using detectImportOptions.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!