Reading lines from text file

조회 수: 4 (최근 30일)
Omer Acil
Omer Acil 2020년 5월 30일
댓글: Walter Roberson 2020년 5월 31일
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에서 Text Data Preparation에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by