Reading data from a text file

Hallo
I usually used load function to read text files. However, this time my text file has the first column as p1 p2 p3 and so on. How can I not read this column and load only the next available columns. I used fopen, fscanf and fclose but it gives an empty array as output.
Thanks in advance.

 Respuesta aceptada

Narges M
Narges M el 25 de Jul. de 2013
Editada: Narges M el 25 de Jul. de 2013

0 votos

use this example:
cfg = fopen('myfile.txt');
line = fgetl(cfg); %this line reads the first line, you can discard it right away
while( ~feof(cfg) )
line = fgetl(cfg);
% read your data here, using textscan for example
end
or use this:
f = fopen('myfile.txt');
E = textscan(f,'%s %f %f %f','delimiter', ' ', 'collectoutput',false,'BufSize',10000);
end

11 comentarios

nl2605
nl2605 el 25 de Jul. de 2013
Thanks a ton. But the problem is that textscan is giving the result as an empty array.
Narges M
Narges M el 25 de Jul. de 2013
it should work on the data you provided unless there are empty lines between each two lines of data.
nl2605
nl2605 el 25 de Jul. de 2013
Well there are two header lines in the text file but I have commented them out. Is that what is creating a problem? Also, I am getting the what I want using importdata. However, the output comes as
'p1 -1846.802371 2576.980471 415.426362'
Do you have any idea how to separate this into a matrix omitting the p1?
Thanks a lot.
Narges M
Narges M el 25 de Jul. de 2013
A = textscan('p1 -1846.802371 2576.980471 415.426362','%s %f %f %f');
nl2605
nl2605 el 25 de Jul. de 2013
But then I will have to put each string everytime. I have a large text that has to be read.
Narges M
Narges M el 25 de Jul. de 2013
with the above line I wanted to show you that it is possible to read the data from the array. You can use the whole data that you acquired using importdata as the input of textscan.
nl2605
nl2605 el 25 de Jul. de 2013
It gives this error.
Error using textscan First input must be of type double or string.
What I am doing is:
data = importdata(Filename,'');
A = textscan(data,'%*c%*d %f %f %f');
This is when it gives the above error
Narges M
Narges M el 25 de Jul. de 2013
Editada: Narges M el 25 de Jul. de 2013
look @ your data first, it's a struct. depending on how it looks, you have to use the appropriate field. for example: data.data .
BTW, I created a txt file like this:
% this
% I have to delete!
p1 -1846.802371 2576.980471 415.426362
p2 -1877.653100 2615.788181 1070.444158
p3 -2645.735560 1613.935330 1093.387751
p4 -2620.204185 1581.891307 552.262732
p5 -3156.164748 1991.576815 502.746179
p6 -3177.459970 2018.327960 954.345230
p7 -2468.674398 2942.854349 933.131245
and using A = importdata(myfile), the values are accessible in matrix format in A.data for me.
nl2605
nl2605 el 25 de Jul. de 2013
Ohh..I thought its coming in just one string form. But since I have not assigned any field names how do I access it?
Narges M
Narges M el 25 de Jul. de 2013
Editada: Narges M el 25 de Jul. de 2013
that's done automatically. refer to "help importdata"
nl2605
nl2605 el 25 de Jul. de 2013
Thanks! got it!

Iniciar sesión para comentar.

Más respuestas (1)

kjetil87
kjetil87 el 25 de Jul. de 2013
Editada: kjetil87 el 25 de Jul. de 2013

0 votos

perhaps you are using fscanf(fid,'%d') ?
Try reading it as characters:
fid=fopen('text.txt');
DATA=fscanf(fid,'%c');
fclose(fid);

4 comentarios

nl2605
nl2605 el 25 de Jul. de 2013
I don't want to read the first column which is in the form of p1 p2 so on..All other data I want to read.
This is how my data looks:
p1 -1846.802371 2576.980471 415.426362
p2 -1877.653100 2615.788181 1070.444158
p3 -2645.735560 1613.935330 1093.387751
p4 -2620.204185 1581.891307 552.262732
p5 -3156.164748 1991.576815 502.746179
p6 -3177.459970 2018.327960 954.345230
p7 -2468.674398 2942.854349 933.131245
nl2605
nl2605 el 25 de Jul. de 2013
I am using importdata right now. But it is giving me the whole data in string format including the header. Like follows:
'p1 -1846.802371 2576.980471 415.426362'
Narges M
Narges M el 25 de Jul. de 2013
E = textscan(f,'%s %f %f %f','delimiter', ' ', 'collectoutput',false,'BufSize',10000);
nl2605
nl2605 el 25 de Jul. de 2013
tried this way too. But it gives E=0.

Iniciar sesión para comentar.

Preguntada:

el 25 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by