How to read txt file in matlab

10 views (last 30 days)
mahmoud el majzoub
mahmoud el majzoub on 30 Mar 2015
Edited: Stephen23 on 1 Apr 2015
Dear all Any one could help me reading the txt file 'specimen1.txt' to plot the load vs extension .For instance
Time Extension Load
(sec) (mm) (N)
0,00000 0,00000 -0,06352
0,00200 0,00000 -0,06571
0,00400 0,00000 -0,06751
0,10400 0,00697 -0,00243
0,20400 0,02255 -0,00033
0,30400 0,03494 -0,00515
0,40400 0,04640 0,02844
0,50400 0,05832 0,03087
0,60400 0,06989 0,05052
0,70400 0,08169 0,05281
0,80400 0,09350 0,02599
0,90400 0,10495 0,06418
1,00400 0,11652 0,00930
1,10400 0,12774 0,07347
1,20400 0,14001 0,08514
1,30400 0,15135 0,03269
1,40400 0,16351 0,09253
1,50400 0,17484 0,09940
I tried with load but i got five columns with random values but i need them in three columns as titled above.Then plot the load column as function as extension.Any one could help please as soon. Regards mahmoud

Answers (7)

per isakson
per isakson on 31 Mar 2015
Edited: per isakson on 31 Mar 2015
See Import data file with comma decimal point to get comma2point_overwrite
&nbsp
Addendum
Then run
comma2point_overwrite('specimen1.txt')
(I assume you have a backup) and
fid = fopen('specimen1.txt');
cac = textscan( fid, '%f%f%f', 'Headerlines,2, 'CollectOutput',1 );
fclose(fid);

mahmoud el majzoub
mahmoud el majzoub on 30 Mar 2015
the file attached here

Image Analyst
Image Analyst on 30 Mar 2015
Did you try importdata() with 2 header lines as an optional input?

mahmoud el majzoub
mahmoud el majzoub on 31 Mar 2015
Yeah i see , i have the version 2010 ,do you some other method please .Thank for your help
  1 Comment
Star Strider
Star Strider on 31 Mar 2015
You should be able to read ‘specimen1d.txt’ without problems using textscan. It used decimal-point rather than decimal-comma formatting.

Sign in to comment.


Michael Haderlein
Michael Haderlein on 31 Mar 2015
When I have this issue, I usually go this way:
fid=fopen('test.txt');
txt=char(fread(fid));
fclose(fid);
headerlines=2;
txt(1:max(find(txt==char(13),headerlines,'first')))=[];
txt=strrep(txt',',','.');
data=str2num(txt);
As only low-level functions are used, I guess this works independently from the Matlab release.

Stephen23
Stephen23 on 31 Mar 2015
Edited: Stephen23 on 31 Mar 2015
Why make this complicated: without changing the original datafile we can simply change the commas for periods inside MATLAB, and then use textread on the resulting string:
>> str = fileread('specimen1.txt');
>> str = regexprep(str,',','.');
>> M = cell2mat(textscan(str,'%f%f%f'))
M =
0 0 0.0076
0.0020 0 0.0067
0.0040 0 0.0040
0.1040 0.0069 0.0294
0.2040 0.0228 -0.0259
0.3040 0.0347 0.0345
...
75.4040 8.7966 -0.1200
75.5040 8.8082 -0.1276
75.6040 8.8201 -0.1791
75.7040 8.8314 -0.1739
75.8040 8.8436 -0.1943
75.9020 8.8547 -0.1583
  5 Comments
Image Analyst
Image Analyst on 31 Mar 2015
Mahmoud's code moved here:
Hello Stefan I tried this but it gave me an emoty matrices
for i=1:8
textFileName = ['specimen' num2str(i) '.txt'];
if exist(textFileName, 'file')
fid = fopen(textFileName, 'rt');
str = fileread(textFileName);
fclose(fid);
str = regexprep(str,',','.');
M = cell2mat(textscan(str,'%f%f%f'))
else
fprintf('File %s does not exist.\n', textFileName);
end
end
Stephen23
Stephen23 on 31 Mar 2015
Edited: Stephen23 on 1 Apr 2015
@mahmoud el majzoub: When I wrote this code I used your example file "specimen1.txt", which contains no headers. If the files have headers, then this method will not work. If you try it with your own example file, it will work. If you have a few header lines, then this code will get rid of them before the conversion:
>> N = 2; % <- pick how many header lines.
>> X = find(str==10);
>> str = str(X(N):end);
A few tips about your code:
  • Do not use i or j as loop variable names, as these are names of the inbuilt imaginary unit.
  • Use sprintf to create the filenames: sprintf('specimen_%i.txt',k)
  • Or instead of creating the filenames and checking if they exist, use dir instead:
S = dir('specimen_*.txt');
N = {S.name};
for k = 1:numel(N)
filename = N{k};
... your code here
end

Sign in to comment.


mahmoud el majzoub
mahmoud el majzoub on 31 Mar 2015
Edited: Image Analyst on 31 Mar 2015
Moved.

Community Treasure Hunt

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

Start Hunting!