How to import txt.-files with different columns and numbers/letters?

5 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Akira Agata
Akira Agata el 30 de Ag. de 2018
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

Más respuestas (1)

Sven
Sven el 30 de Ag. de 2018
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.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by