How to import txt.-files with different columns and numbers/letters?
5 次查看(过去 30 天)
显示 更早的评论
I want to import two different txt.-files, one with 7 columns and the other one with 9, both have around 1.500.000 rows.
One looks like this:
C:\Bose\....
"Points","Elapsed Time ","Scan Time ","Disp ","Load 3","T1_Control","T2_Chamber",
" ","Sec ","Sec ","mm ","N ","C ","C ",
1, 0.00000, 0.00000, -3.276, 1.658, -40.0, -41.5,
2, 0.00060, 0.00060, -3.272, 1.711, -40.0, -41.5,
Sometimes instead of numbers there are xxxx in some rows.
I tried to import it with:
[filename, path] = uigetfile({'*.txt'});
selectedfile = fullfile(path,filename);
[a] = importdata(selectedfile,',');
It works as long as there are just numbers, but when there are xxxx in a row, the file is just imported to this row and the rest of the file is ignored.
How do I continue the import if there are xxxx in some rows?
0 个评论
采纳的回答
Akira Agata
2018-8-30
If your data file looks like this:
C:\Bose\....
"Points","Elapsed Time ","Scan Time ","Disp ","Load 3","T1_Control","T2_Chamber",
" ","Sec ","Sec ","mm ","N ","C ","C ",
1, 0.00000, 0.00000, -3.276, 1.658, -40.0, -41.5,
2, 0.00060, 0.00060, -3.272, 1.711, -40.0, -41.5,
3, 0.00060, 0.00060, -3.272, 1.711, xxxx, -41.5,
4, 0.00060, 0.00060, -3.272, 1.711, -40.0, -41.5,
The following code can read them and convert exceptional characters (like 'xxxx') with NaN.
d = readtable(yourFile,'HeaderLines',3,'ReadVariableNames',false,'Format','%s%s%s%s%s%s%s');
d = varfun(@str2double,d);
The result looks like:
>> d
d =
4×7 table
str2double_Var1 str2double_Var2 str2double_Var3 str2double_Var4 str2double_Var5 str2double_Var6 str2double_Var7
_______________ _______________ _______________ _______________ _______________ _______________ _______________
1 0 0 -3.276 1.658 -40 -41.5
2 0.0006 0.0006 -3.272 1.711 -40 -41.5
3 0.0006 0.0006 -3.272 1.711 NaN -41.5
4 0.0006 0.0006 -3.272 1.711 -40 -41.5
更多回答(1 个)
Sven
2018-8-30
Try using readtable() instead. This will import all your data, but changes all data to character vectors. If you want to further use the data, you need to convert the table into a normal array, or cell array in this case, with table2array().
[filename, path] = uigetfile({'*.txt'});
selectedfile = fullfile(path,filename);
A = readtable(selectedfile,'Delimiter',',','ReadVariableNames',false);
B = table2array(A);
If you need an array you have to use a loop to move each element into a normal array. The table2array() function should actually convert it directly into an array, but it doesn't seem to work, so table2array() and table2cell() both convert it into a cell array.
This is based on my experience, so it might not be the most effective way.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!