Stroring data from text file into cell array
3 次查看(过去 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!