How to import txt.-files with different columns and numbers/letters?
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (1)
Sven
il 30 Ago 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.
1 Commento
Eric Helfer
il 30 Ago 2018
Categorie
Scopri di più su Data Type Conversion in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!