Converting a text file to a Matlab matrix/variable

8 vues (au cours des 30 derniers jours)
Bernard
Bernard le 11 Fév 2012
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

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 11 Fév 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 commentaires
Bernard
Bernard le 11 Fév 2012
Thank you so much, Andrei for the answer! This is a super big help for me!
Bernard
Bernard le 11 Fév 2012
Hi again Andrei!
I tried the scripts above, experimented on BufSize for textscan to accommodate 1500002 characters per line using a two-row data file. A BufSize of 160000 produced the necessary result. However, when I try to run it on my "real" data having 633 lines, I get this message:
Caught std::exception Exception message is:bad allocation.
I tried Googling around but found no answer close enough. By the way I tried typing in 'memory' at the prompt and I got this
Maximum possible array: 199 MB (2.089e+008 bytes) *
Memory available for all arrays: 713 MB (7.480e+008 bytes) **
Memory used by MATLAB: 1064 MB (1.116e+009 bytes)
Physical Memory (RAM): 2031 MB (2.129e+009 bytes)
The above may help in resolving this issue.
Thanks

Connectez-vous pour commenter.

Plus de réponses (1)

farouk benseghir
farouk benseghir le 26 Mar 2017
heyy;
i have prblm plz help me iwant to open this fichier txt in mtlab any one teach me how ido this!

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by