フィルターのクリア

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

4 ビュー (過去 30 日間)
Eric Helfer
Eric Helfer 2018 年 8 月 30 日
編集済み: Eric Helfer 2018 年 8 月 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?

採用された回答

Akira Agata
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 件のコメント
Eric Helfer
Eric Helfer 2018 年 8 月 30 日
編集済み: Eric Helfer 2018 年 8 月 30 日
Thanks for your answer! It works with readtable

サインインしてコメントする。

その他の回答 (1 件)

Sven
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.
  1 件のコメント
Eric Helfer
Eric Helfer 2018 年 8 月 30 日
Thanks for your answer!
readtable helped

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by