reding specific elements from .txt file
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi
I want to read specific elements from .txt file. I am reading data from several files and they are having different no. of rows but same no. of columns. I want to read all the elements in 12th column and in rows which come after 10 the row.
So matrix which I am going to read will be a column matrix but of different length depending upon no. of rows in respective file.
3 commentaires
Réponse acceptée
TAB
le 23 Sep 2011
ReadCol=7;
ReadStartRow=4;
fh=fopen('test.txt');
txtcell=textscan(fh,'%s','delimiter','\n');
RowInFile=length(txtcell{1});
if(RowInFile<ReadStartRow)
error('No of rows in text file are less than as expected by you.');
end
MyData=zeros(RowInFile-ReadStartRow+1,1);
R=1;
for ro=ReadStartRow:RowInFile
Arr=cell2mat(textscan(txtcell{1}{ro},'%f','delimiter',','))';
Mydata(R,1)=Arr(ReadCol);
R=R+1;
end
0 commentaires
Plus de réponses (1)
TAB
le 22 Sep 2011
StartRow=1;
EndRow=6;
StartCol=1;
EndCol=8;
if(StartRow>EndRow || StartCol>EndCol || any([StartRow EndRow StartCol EndCol]==0))
error('Indexing Error');
end
fh=fopen('YourFile.txt');
txtcell=textscan(fh,'%s','delimiter','\n');
if(length(txtcell{1})<EndRow)
error('No of rows in text file are less than as expected by you.');
end
R=1;
MyData=zeros(EndRow-StartRow+1,EndCol-StartCol+1);
for i=StartRow:EndRow
Arr=cell2mat(textscan(txtcell{1}{i},'%f','delimiter',','))';
Mydata(R,:)=Arr(1,StartCol:EndCol);
R=R+1;
end
3 commentaires
TAB
le 23 Sep 2011
Sorry for misunderstanding your need.
Please see my next answer.
I am again confused, either you want to read 7th column or 6th column. I am considering it 7th.
Voir également
Catégories
En savoir plus sur Data Import and Analysis dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!