Load a txt file:
5 次查看(过去 30 天)
显示 更早的评论
Hi, I have the following question: Supose I have a txt file, myfile.txt and it look like this:
*/header
fff.235
T 531VV951 211VV015L1 211VV035 211VV101
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
T
531VV951
211VV015L1
211VV035
211VV101 */
I want to know how can I skip first 3 lines, i.e
/header;fff.235;T 531VV951...
and the last 5 lines (i.e T; 531VV951; ) I was deleting manually those lines and used after AA=load('myfile.txt') and it was working fine. Alternatively I used data= dlmread(myfile.txt, ',',3 ,); but I don't know how should I do it for the last 5 lines which I have to skip. There are now 50 files and matrix is 30000x30. Any idea would be appreciated. Thank you
2 个评论
采纳的回答
Stephen23
2015-9-29
编辑:Stephen23
2015-9-30
You could use the function textscan, which lets you select the headerlines to ignore (so you don't need to delete any lines by hand), and also define the number of rows that should be read into MATLAB.
EDIT to match newly uploaded textfile.
This code reads that text file, including the header, the matrix, and the trailing data. It also adjusts automatically to the number of columns.
fid = fopen('myexamplefile.txt','rt');
line1 = fgetl(fid);
hdr = regexp(fgetl(fid),'\S+','match');
fmt = repmat('%f',1,numel(hdr));
C = textscan(fid,fmt);
D = textscan(fid,'%s%s%s%s',1,'HeaderLines',1);
fgetl(fid);
E = textscan(fid,'%s%f%f%f%f',1,'HeaderLines',1);
fclose(fid);
0 个评论
更多回答(1 个)
Walter Roberson
2015-9-29
filerows = 30000;
filecols = 30;
fmt = repmat('%f', 1, filecols);
fid = fopen('myfile.txt', 'rt');
datacell = textscan(fid, fmt, filerows, 'HeaderLines', 3, 'CollectOutput', 1);
fclose(fid);
result = datacell{1};
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!