Reading text from a txt file into a cell array

2 visualizzazioni (ultimi 30 giorni)
Manikandan
Manikandan il 10 Mar 2011
The text file (trial.txt) is as follows:
line1
line2
line3
and I used k = fileread('trial.txt') to get the text into a cell array 'k'. Now the problem is, it takes the whole text as a single line. But I want it to be separate rows in the array (for each new line). Am not sure about how I should do that. I tried using textscan also and ended up getting nothing from the text file. My knowledge in MATLAB is limited. Please help me out of this. Thanks

Risposte (3)

Walter Roberson
Walter Roberson il 10 Mar 2011
You can use regexp with splitting. Please see this example

Matt Fig
Matt Fig il 10 Mar 2011
What does the data on each line look like? Does it follow a pattern? Post one of the lines if all lines follow a similar pattern, because you should be able to do this with TEXTSCAN.

Manikandan
Manikandan il 10 Mar 2011
The text looks like this, a simple paragraph.
The various fields that the tool covers:
Fluid machinery
Electro mechanical devices
etc
I used textread command with newline \n for whitespace. Now it works fine. Thanks much for your reply Walter and Matt.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by