How to read txt file in matlab
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (7)
per isakson
am 31 Mär. 2015
Bearbeitet: per isakson
am 31 Mär. 2015
 
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);
0 Kommentare
Image Analyst
am 30 Mär. 2015
Did you try importdata() with 2 header lines as an optional input?
0 Kommentare
mahmoud el majzoub
am 31 Mär. 2015
1 Kommentar
Star Strider
am 31 Mär. 2015
You should be able to read ‘specimen1d.txt’ without problems using textscan. It used decimal-point rather than decimal-comma formatting.
Michael Haderlein
am 31 Mär. 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.
0 Kommentare
Stephen23
am 31 Mär. 2015
Bearbeitet: Stephen23
am 31 Mär. 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 Kommentare
Image Analyst
am 31 Mär. 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
am 31 Mär. 2015
Bearbeitet: Stephen23
am 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
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!