How to import certain lines of a dat file into matlab
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi
Here is my text file in notepad. I just want to import -299.0625,57.52286903 from the third line in Sample 1, fourth line: 66.11002865,24.76844395, fifth line: 48.54979868,17.81016143 from Sample 1 and 5.837404251,13.84681828 from first line of Sample 2, 5.610181444,11.92740209 from second line of Sample 2 and 5.23138808,11.4906786 from the last line of Sample 2.
Could you please tell me how can I do this? This is just a small part of lines... There are totally 500 Samples
Material 1,,x,y,c,phi
,,,,,
Sample 1,,0.986842105,-299.0625,57.52286903,28.20109657
,,2.960526316,-299.0625,66.11002865,24.76844395
,,4.934210526,-299.0625,48.54979868,17.81016143
,,,,,
Sample 2,,0.986842105,-299.0625,5.837404251,13.84681828
,,2.960526316,-299.0625,5.610181444,11.92740209
,,4.934210526,-299.0625,5.23138808,11.4906786
댓글 수: 0
답변 (1개)
Mihir
2023년 6월 27일
In MATLAB, you can use the "textscan" function to read the data from the text file and extract the desired values. Below code snippet can help you in achieving the desired result, you can take idea from it and can implement it according to your needs
fid = fopen('your_file.txt', 'r');
% Read the lines from the file
lines = textscan(fid, '%s', 'Delimiter', '\n');
lines = lines{1};
% Close the file
fclose(fid);
% Extract the desired values
sample1_line3 = strsplit(lines{4}, ',');
sample1_line3 = sample1_line3(3:5);
sample1_line4 = strsplit(lines{5}, ',');
sample1_line4 = sample1_line4(3:4);
sample1_line5 = strsplit(lines{6}, ',');
sample1_line5 = sample1_line5(3:4);
sample2_line1 = strsplit(lines{9}, ',');
sample2_line1 = sample2_line1(3:4);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!