How to import certain lines of a dat file into matlab

11 views (last 30 days)
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

Answers (1)

Mihir
Mihir on 27 Jun 2023
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);

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!