Stroring data from text file into cell array
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Cris LaPierre
2020 年 11 月 12 日
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 件のコメント
Cris LaPierre
2020 年 11 月 13 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!