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.

 Accepted Answer

Narges M
Narges M on 25 Jul 2013
Edited: Narges M on 25 Jul 2013
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 Comments

Thanks a ton. But the problem is that textscan is giving the result as an empty array.
it should work on the data you provided unless there are empty lines between each two lines of data.
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.
A = textscan('p1 -1846.802371 2576.980471 415.426362','%s %f %f %f');
But then I will have to put each string everytime. I have a large text that has to be read.
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.
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 on 25 Jul 2013
Edited: Narges M on 25 Jul 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.
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 on 25 Jul 2013
Edited: Narges M on 25 Jul 2013
that's done automatically. refer to "help importdata"
Thanks! got it!

Sign in to comment.

More Answers (1)

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

4 Comments

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
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'
E = textscan(f,'%s %f %f %f','delimiter', ' ', 'collectoutput',false,'BufSize',10000);
tried this way too. But it gives E=0.

Sign in to comment.

Asked:

on 25 Jul 2013

Community Treasure Hunt

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

Start Hunting!