Read two lines from a file and repeat for next lines

4 次查看(过去 30 天)
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.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-12-3
编辑:Ameer Hamza 2020-12-3
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by