How to read one number from multiple text files
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have been struggeling with finding a way to read 1000 text files into matlab. The text files have letters in them as well, however from every text file I only want one number. I have tried dlmread, that doesnt work. I Have tried fopen, I couldnt figure out how can I specifiy the numer location (which row and couloumn).
I would very much appreciate it if someone knows a way!!
5 个评论
dpb
2020-3-18
That's a trivial file format to read presuming that is the beginning of the file...
data=readmatrix('Yourfilename.txt','NumHeaderLines',1,'Range','E6');
the overhead of the specific element read may/may not be significant; the previous suggestion would have just been
data=readmatrix('Yourfilename.txt','NumHeaderLines',1); % read full data array
data=data(5,6); % keep the wanted data
采纳的回答
Sindhu Karri
2020-3-23
This is an alternate solution
fileID = fopen('textfile.txt','r');%textfile to be included in the current folder path
formatSpec = '%d %d %d %d';
sizeA = [4 6];%dimensions of the data to be read
A = fscanf(fileID,formatSpec,sizeA);
A=A'
b=A(6,3)% to get the specified number
fclose(fileID);
Refer to below link
2 个评论
Walter Roberson
2020-3-23
You need an initial fgets(fileID) to skip the header line.
What is the reasoning for bothering to take the transpose? Why not omit it and use A(3,6) ?
更多回答(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!