Converting a text file to a Matlab matrix/variable
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bernard
am 11 Feb. 2012
Beantwortet: farouk benseghir
am 26 Mär. 2017
Hi everyone!
I've got this kind of data file below, having around 633 lines (which I want to convert to rows) with each line having 75,000 characters (these characters include spaces and the letter "o", which I want to convert into Matlab variable columns). Each line runs running either with space or the letter "o" and does not have a delimeter between the characters. Textscan cannot read this since it does not have a delimeter in between. A sample is below:
"oooo o o" --- first line
"o o o o" --- second line
I only showed two lines with 21 characters (including the spaces). With this sample I want to have a Matlab variable with two rows and 21 columns such that if there is "o" I would like the Matlab variable to have the value 1, otherwise it should be zero when it encounters a space. I have come up with this code below. However, it so inefficient and very slow (and does not give results corresponding to thousands of columns), probably because it has to open the same file 75,000 to generate 75,000 columns.
------------------------------------------
for n=1:50,000;
fid=open('datamap.txt')
for k=1:633;
tline=fgets(fid);
if isletter(tline(n))==1;
Matlab(k,n)=1;
else Matlab(k,n)=0;
end
end
fclose all
end
----------------------------------------------
It seems to work for about a few hundred characters but does not with my 75,000 character per line. Please help
Thanks,
Bernard
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 11 Feb. 2012
t = ['oooo o o'
'o o o o']
out = reshape(regexprep(t(:)',{'o',' '},{'1' '0'}),size(t))-'0'
OR
fid = fopen('yourtextfile.txt');
t = textscan(fid,'%21c');
fclose(fid);
out = reshape(regexprep(t{1}(:)',{'o',' '},{'1' '0'}),size(t{1}))-'0'
2 Kommentare
Weitere Antworten (1)
farouk benseghir
am 26 Mär. 2017
heyy;
i have prblm plz help me iwant to open this fichier txt in mtlab any one teach me how ido this!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Text Data Preparation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!