Reading/Extracting data from a text file in a certain format using Matlab

Hi,
I am a beginner in Matlab. I need to extract some required data [ I index, J index of cells and their lengths(DX) and widths(DY)] from the attached text file in the following formate:
I J DX DY
2 2 35.53 51.93
3 2 35.53 51.93
4 2 35.53 51.93
5 2 35.53 51.93
.............................................
.............................................
196 199 35.53 51.93
197 199 35.53 51.93

4 comentarios

what is the format of data present in text file?
It is a .txt file. Please see the attached text file in the question. Thanks.
But data is not clear in text file. Below are the first few lines of text file. Could you explain what each line corresponds to?
2 2 963354.2231605511624366963389.7596605512080714963389.7596605512080714963354.2231605511624366
1383067.78205397375859321383067.78205397375859321383119.72205397370271381383119.7220539737027138
963371.9914105511270463 1383093.7520539737306535
35.5365000000409808 51.9399999999999977
It looks like the first line corresponds to i j and the fifth to Dx Dy.

Iniciar sesión para comentar.

 Respuesta aceptada

Hi Shuvashish,
If i'm correct in my understanding of your data, it looks like you want to take the first and fifth line of data for every 5 lines in your text file.
You could try reading the text file line by line using fgetl(). This returns the next line of your file as a character vector, then use str2num() to convert your character vector to a 1x2 array in this case. I've added in conditions to store only the lines you specified. Note that you need to have waterscape2.txt in your current working directory or else filename has to be changed, for example filename='C:\...\waterscape2.txt'.
filename='waterscape2.txt'
fid = fopen(filename);
fgetl(fid); %read first line of text file
tline=fgetl(fid); %read second line
c=0; ij=[]; DxDy=[];
while ischar(tline)
c=c+1;
if mod(c+4,5)==0 % store when c=1,6,11,...,etc.
ij(end+1,1:2)=str2num(tline);
end
if mod(c,5)==0 % store when c=5,10,15,...,etc.
DxDy(end+1,1:2)=str2num(tline);
end
tline = fgetl(fid);
end
fclose(fid)
data=[ij DxDy];
Aside from fgetl there is also some relevant documentation here for reading data line-by-line.

1 comentario

thank you so much for your help, It worked ! However, would you please explain how the "end+1" works in storing data inside ij and DXDY :) Again, how to modify the code If I want to insert another two columns in the matrix with some constant values (i.e. 5 & 3). ?
Let's think the output matrix should be the following:
I J DX DY DM DN
2 2 35.53 51.93 5 3
3 2 35.53 51.93 5 3
4 2 35.53 51.93 5 3
5 2 35.53 51.93 5 3
.................................................... ...............
...................................................................
196 199 35.53 51.93 5 3
197 199 35.53 51.93 5 3

Iniciar sesión para comentar.

Más respuestas (1)

Turlough Hughes
Turlough Hughes el 8 de Oct. de 2019
Editada: Turlough Hughes el 8 de Oct. de 2019
No problem.
On your first question; The last index of an array can be specified by calling the index end. end + 1 means we add an additional row at the end of the arrays ij and DxDy to save the new lines read from your file. It might be helpful for you to look up matrix indexing in the documentation or the following video.
For the second question, one way is to do the following:
DMDN = ones(size(ij));
DMDN(:,1)=DMDN(:,1).*5;
DMDN(:,2)=DMDN(:,2).*3;
data = [data DMDN];

3 comentarios

Hi, Turlough,
Thanks a lot for your prompt reponse. You are such a helpful person. By the way, If I want to read the fourth line instead of the fift line, how should I change the code?
Lets think the output be like this:
I J DX DY
2 2 963371.99 1383093.75
3 2 963407.53 1383093.75
.................................................... ...............
...................................................................
197 199 970301.61 1393325.93
Thanks.
Thanks Turlough,
I have figured it out.
filename='waterscape2.txt'
fid = fopen(filename);
fgetl(fid); %read first line of text file
tline=fgetl(fid); %read second line
c=0; ij=[]; DxDy=[];
while ischar(tline)
c=c+1;
if mod(c+4,5)==0 % store when c=1,6,11,...,etc.
ij(end+1,1:2)=str2num(tline);
end
if mod(c+1,5)==0 % store when c=5,10,15,...,etc.
DxDy(end+1,1:2)=str2num(tline);
end
tline = fgetl(fid);
end
fclose(fid)
data=[ij DxDy];
save('efdclxly.txt','data','-ascii');
Change
mod(c,5)==0
to
mod(c+1,5)==0

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 1 de Oct. de 2019

Editada:

el 8 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by