How to read txt file in matlab
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(7 个)
per isakson
2015-3-31
编辑:per isakson
2015-3-31
 
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 个评论
mahmoud el majzoub
2015-3-31
1 个评论
Star Strider
2015-3-31
You should be able to read ‘specimen1d.txt’ without problems using textscan. It used decimal-point rather than decimal-comma formatting.
Michael Haderlein
2015-3-31
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 个评论
Stephen23
2015-3-31
编辑:Stephen23
2015-3-31
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 个评论
Image Analyst
2015-3-31
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
2015-3-31
编辑:Stephen23
2015-4-1
@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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!