troubles to load .txt-file in matlab

4 Ansichten (letzte 30 Tage)
Bruno Schmid
Bruno Schmid am 11 Apr. 2017
Kommentiert: Star Strider am 11 Apr. 2017
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 :-)

Akzeptierte Antwort

Star Strider
Star Strider am 11 Apr. 2017
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 Kommentare
Bruno Schmid
Bruno Schmid am 11 Apr. 2017
Great!! Thanks a lot. That worked fine :-)
Best
Star Strider
Star Strider am 11 Apr. 2017
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by