Converting a text file to a Matlab matrix/variable
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Andrei Bobrov
2012-2-11
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'
更多回答(1 个)
farouk benseghir
2017-3-26
heyy;
i have prblm plz help me iwant to open this fichier txt in mtlab any one teach me how ido this!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!