MATLAB Answers

Read in a plain/text file in Matlab

16 views (last 30 days)
Eli Dim
Eli Dim on 28 Jul 2015
Edited: per isakson on 28 Jul 2015
I have a plain/text file which contains the following information:
"Dukovany","Temelin","Prunerov","Pocerady","Chvaletice","Detmarovice","Tusimice II","Melnik II&III","Vresova","Opatovice","Melnik I","Kladno","Ostrava","Komorany","Ledvice","Tisova I","Trebovice","Porici","Plzen","Praha","Steti","Litvinov","Hodonin","Kladno II","Zlin","Krystovofy","Horni","Dlouhe","Dalesice","Orlik","Slapy","Lipno","Stechovice II","Kamyk","Stechovice I","Strekov","Vranov","Vrane","Nechranice","Ralsko","Veprek","Sevetin","Brno","Mimon","Vranovska","Stribro","Chomutov","Uhersky Brod","Chrudichromy","Letkow","Line"
I want to read it in Matlab. I tried
infile = 'PowerPlantNames';
data = load(infile,'-ascii');
But I get the following error: Error using load Unknown text on line number 1 of ASCII file PowerPlantNames ""Dukovany"". How can I read in my information?
  1 Comment
Eli Dim
Eli Dim on 28 Jul 2015
Part of the problem I solved (load can't be used in this case). Using the following command I was able to input the information:
filename = 'PowerPlantNames.txt';
powerplantnames = importdata(filename);
I attached the outputut (powerplantnames). How can I quickly separate the strings?

Sign in to comment.

Accepted Answer

per isakson
per isakson on 28 Jul 2015
Edited: per isakson on 28 Jul 2015
Try
>> out = cssm();
>> out{1}(12:14)
ans =
'Kladno'
'Ostrava'
'Komorany'
where
function cac = cssm()
fid = fopen( 'PowerPlantNames.txt' );
cac = textscan( fid, '%q', 'Delimiter',',' );
fclose( fid );
end
and PowerPlantNames.txt contains the data of your question
Or using the file 1.mat
>> load 1
>> cac = textscan( powerplantnames{1}, '%q', 'Delimiter',',' );
>> cac{1}(12:14)
ans =
'Kladno'
'Ostrava'
'Komorany'

More Answers (1)

Muthu Annamalai
Muthu Annamalai on 28 Jul 2015
Original question is answered by
  1. doc fileread
See split manipulation functions
  1. doc strsplit
  2. doc regexprep

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by