Code to import every 50th line of .csv isn't working
2 次查看(过去 30 天)
显示 更早的评论
File is 6 columns wide, n rows long. 2 header lines. Tried this code to extract every 50th line (and keep it as its own matrix). Not sure why it isn't working.
clc
clear
per_line = 6;
every_nth_line = 50;
fmt = [repmat('%f', 1, per_line), repmat('*%f', 1, per_line*(every_nth_line-1))];
fid = fopen('Plain_VHB_Cyclic_Test_1.csv', 'rt');
datacell = textscan(fid, fmt, 'delimiter', ',\n', 'HeaderLines', 2, 'CollectOutput', 1);
fclose(fid);
C = datacell{1};
0 个评论
采纳的回答
dpb
2022-10-4
That's hard way to go about it --
data=readmatrix('Plain_VHB_Cyclic_Test_1.csv','numheaderlines',2);
data=data(1:50:end,:);
2 个评论
dpb
2022-10-4
It's almost always easier to code and as fast in operation to just read the file and do whatever is the selection in memory than try to code to read bits 'n pieces out of a file, whether it's lines or columns or whatever.
About the only time it isn't the more effective method is if the file is too large to fit in memory
更多回答(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!