Reading lines from text file

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 commentaires

Walter Roberson
Walter Roberson le 30 Mai 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
Omer Acil le 31 Mai 2020
I was trying your suggestions but could'nt deal with it.By the way I dont have spaces in books names. There are underscores instead of spaces.
dpb
dpb le 31 Mai 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.
Omer Acil
Omer Acil le 31 Mai 2020
Modifié(e) : Omer Acil le 31 Mai 2020
Thank you for your respond. I tried readtable but I had couple issues which are: I can not put them in order it lines them like var1,var2,var3 but I want to line them like var2,var3,var1. And the other issue is that it always takes the first line of the text file even though I try to change the first line. First line(and I assume they are variables now) is FC123 Harry_Potter and Fiction. Is there any way to change variable names? (I forgot to mention It also gives me an additional variable which I do not what it says var4_1 and it gives me 0x0 char
dpb
dpb le 31 Mai 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...).
t = readtable('YourFile.txt', 'ReadVariableNames', false);
newt = t(:,[2 3 1]);

Connectez-vous pour commenter.

Réponses (0)

Catégories

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by