reding specific elements from .txt file

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 comentarios

TAB
TAB el 21 de Sept. de 2011
Please give example of your data.
What is its content, number or string?
How columns are separated ?
Nikhil CD
Nikhil CD el 22 de Sept. de 2011
All are numbers, delimiter is comma(,).
For example( I am assuming data of 6 columns and 8 rows)
1,2.5,45,-5,3.25,0,13,6
2.5,86,25,-4,0,0,12.5,2
2.1,-5,1.5,2.3,0,0,2.45,25
1,2.5,45,-5,0,0,1.23,6
2.5,86,25,-4,0,0,2.5,2
2.1,-5,1.5,2.3,0,0,0.45,25
In above case I need elements: 1.23,2.5,0.45
But in other files no of rows may be different and accordingly no of elements will vary.( in data of 8*8 i will input 5 elements(starting from row4))
Nikhil CD
Nikhil CD el 22 de Sept. de 2011
Change in last sentence...(in data of 8*6 I will need elements 5 elements(starting from row4))

Iniciar sesión para comentar.

 Respuesta aceptada

TAB
TAB el 23 de Sept. de 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

Más respuestas (1)

TAB
TAB el 22 de Sept. de 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 comentarios

Nikhil CD
Nikhil CD el 23 de Sept. de 2011
I think I could not convey my problem to you properly.
I want to input all the elements in 7th column but starting from 4th row.
And also I am taking input from several files.
So depending upon the no. of rows in a file, no of elements to be read will be different.(Please note that no. of columns in all the files is same)
If file contains 9rows then I will read 6 elements(from 6th column and starting from 4th row)
If file contains 5 rows then I will read 2 elements(from 6th column and starting from 4th row)
TAB
TAB el 23 de Sept. de 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.
Nikhil CD
Nikhil CD el 26 de Sept. de 2011
Yes. Its Ok..I got your logic. I can do further changes needed.
Thank you for your answer.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import and Analysis en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 21 de Sept. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by