How to read two words from a text file?
3 次查看(过去 30 天)
显示 更早的评论
As the question says, what is the easiest way to read just the first two words from a text file in MATLAB? There is more data in the text file, but I don't need to read them. Bonus points if I can store them in a string easily.
0 个评论
采纳的回答
Ameer Hamza
2018-4-26
fID = fopen(filename, 'r');
firstLine = fgets(fID);
fclose(fID);
spaceLocation = strfind(firstLine, ' ');
requiredWords = firstLine(1:spaceLocation(2)-1);
If your text file has no new lines, then you can also specify the number of character to read. For example, you only want first two words. It is safe to assume that first two words will be less than 30 characters in length. You can do it like this
firstLine = fgets(fID, 30); % replace 30 with any number you see appropriate.
4 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!