Reading lines from text file

10 次查看(过去 30 天)
Omer Acil
Omer Acil 2020-5-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
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
Walter Roberson 2020-5-31
t = readtable('YourFile.txt', 'ReadVariableNames', false);
newt = t(:,[2 3 1]);

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by