Reading lines from text file
10 次查看(过去 30 天)
显示 更早的评论
Hello everyone;
I have a homework to do but I am stuck at a part of it and I need your help. In the part I am stuck at is to read lines from txt file and but it on matrix. I have textfile of a list of books. This file includes three parts which are; name of the book, code of the book, and the genre. So when I use this function below I get three different arrays (41x1 cell array).
[code,name,genre]=textread('books.txt','%s %s %s')
I want my code to list books in order like:
Book name1, Genre1, Code1
Book name2, Genre2, Code2
Book name3, Genre3, Code3
6 个评论
dpb
2020-5-31
Read the input description for readtable (or any MATLAB function) carefully...it will describe all the options available to control default behavior for specific cases. Of course you can reorder the data in any order you so desire once you have it in memory; i/o routines simply read the data the way it is in the file; if you want/need something different then that's the next step.
As far as empty variables/columns, that would be indicative the input file may be malformed with an extra column separator like a trailing comma.
Since we can't see the input file here, we can't really say much about that...
There's also detectImportOptions which can and does a much more thorough job of trying to figure out the file structure and returns an import object you can customize for purpose. In general, though, one would figure that's a level of sophistication above that of typical homework so it's probably something much more trivial that is going on...but again, not being able to see the input file form, we're only guessing (well-educated guesses based on described symptoms, but still...).
Walter Roberson
2020-5-31
t = readtable('YourFile.txt', 'ReadVariableNames', false);
newt = t(:,[2 3 1]);
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!