Read two lines from a file and repeat for next lines

2 views (last 30 days)
Hello everybody
i have a file part like this:
1, 695, 400, 8, 519, 2721, 2426, 2034, 2545, 6082, 6081, 6080, 6079, 6083, 6084, 6085,
6086, 6088, 6087, 6089, 6090
2, 694, 117, 678, 730, 2720, 2143, 2704, 2756, 6094, 6093, 6092, 6091, 6095, 6096, 6097,
6098, 6100, 6099, 6101, 6102
3, 116, 679, 726, 724, 2142, 2705, 2752, 2750, 6106, 6105, 6104, 6103, 6107, 6108, 6109,
6110, 6112, 6111, 6113, 6114
4, 724, 722, 683, 116, 2750, 2748, 2709, 2142, 6117, 6116, 6115, 6103, 6118, 6119, 6120,
6110, 6114, 6121, 6122, 6112
5, 115, 728, 692, 682, 2141, 2754, 2718, 2708, 6126, 6125, 6124, 6123, 6127, 6128, 6129,
6130, 6132, 6131, 6133, 6134
Now i would like to read the first two lines and add to a cell like:
for b=(start_elements(i)+1):(end_elements(i)-1)
Element{i}(p,:)=strread(FFLINES{b}, '%f', 'delimiter', ',');
p=p+1;
end
In the end i will have a cell Element{1,1} with all the 21 elements but i cannot figure out how to do it.

Accepted Answer

Ameer Hamza
Ameer Hamza on 3 Dec 2020
Edited: Ameer Hamza on 3 Dec 2020
It is more efficient to read everything in a single call. I/O operations on disk can be a bottleneck, so that should be reduced as much as possible
str = fileread('data.txt');
data = textscan(str, '%f', 'Delimiter', ',');
data = reshape(data{1}, 21, []).'
data.txt is attached.

More Answers (0)

Categories

Find more on Characters and Strings 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!