troubles to load .txt-file in matlab

4 ビュー (過去 30 日間)
Bruno Schmid
Bruno Schmid 2017 年 4 月 11 日
コメント済み: Star Strider 2017 年 4 月 11 日
Hello I've tried to load / import a simple text file into matlab workspace. Therefore I've tried several functions such as "load", "importdata", "fileread" or "dlmread". For dlmread I got the following error: Mismatch between file and format string. Trouble reading 'Numeric' field from file (row number 1, field number 2) ==> ;8405;-0,00137;1,4E-5;-0,058833;-0,004609;0;3,011508;\n I guess that happend because of the "4E-5" term... When using "load" I got Number of columns on line 3 of ASCII file V17028_TipFracture_torque to failure or -1mm_DBG_002.txt must be the same as previous lines. I've attached the .txt-file. I could import the data but they aren't very well organized. Delimiter is ";". Using these functions with the imput argument ";" after the filename doesn't realy work. Any suggestions? Thanks a lot :-)

採用された回答

Star Strider
Star Strider 2017 年 4 月 11 日
Try this:
fidi = fopen('V17028_TipFracture_torque to failure or -1mm_DBG_001.txt','rt');
dscc = textscan(fidi, '%f%s%s%s%s%s%s%s%*s', 'Delimiter',';', 'CollectOutput',1);
dscp = cellfun(@(x)strrep(x,',','.'), dscc(2), 'Uni',0);
Dc = cellfun(@(x)str2num(x), dscp{:}, 'Uni',0);
Data = [dscc{1}, cell2mat(Dc)];
One problem is that the decimal separator is a comma, so it is necessary to read columns 2:7 in as strings, then use strrep to change the decimal separator to a period or point, then use str2num and cell2mat to convert the cell array of strings to a numeric array. The ‘Data’ matrix are the complete numeric data. (They appear to have been imported correctly.) The first row has an extra column that I told the textscan function to ignore so the rest of the data would convert correctly.
  2 件のコメント
Bruno Schmid
Bruno Schmid 2017 年 4 月 11 日
Great!! Thanks a lot. That worked fine :-)
Best
Star Strider
Star Strider 2017 年 4 月 11 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by