Reading lines from text file
Show older comments
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 Comments
Walter Roberson
on 30 May 2020
textread() has been recommended against for quite a while.
There are multiple ways of proceeding. textscan() is one; readtable() is another. fileread() and regexp() is another.
Caution: Using %s format with textread() will stop processing at the first whitespace, which would mean that the book name would not be able to have multiple words. Using %s format with textscan() by default stops processing at the first whitespace, but you can use the Delimiter and Whitespace options to control that.
Omer Acil
on 31 May 2020
dpb
on 31 May 2020
Unless the homework rules prohibit it, I'd suggest using readtable altho you could easily-enough combine the three arrays you have now into a single cell array of three columns. You might again (presuming rules don't interfere, often homework puts in artificial constraints to either illustrate the hard way first or out of inexperience in the person writing the question) find the newer string class of use.
dpb
on 31 May 2020
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
on 31 May 2020
t = readtable('YourFile.txt', 'ReadVariableNames', false);
newt = t(:,[2 3 1]);
Answers (0)
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!