Effacer les filtres
Effacer les filtres

Reading data from a text file

18 vues (au cours des 30 derniers jours)
nl2605
nl2605 le 25 Juil 2013
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.

Réponse acceptée

Narges M
Narges M le 25 Juil 2013
Modifié(e) : Narges M le 25 Juil 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 commentaires
Narges M
Narges M le 25 Juil 2013
Modifié(e) : Narges M le 25 Juil 2013
that's done automatically. refer to "help importdata"
nl2605
nl2605 le 25 Juil 2013
Thanks! got it!

Connectez-vous pour commenter.

Plus de réponses (1)

kjetil87
kjetil87 le 25 Juil 2013
Modifié(e) : kjetil87 le 25 Juil 2013
perhaps you are using fscanf(fid,'%d') ?
Try reading it as characters:
fid=fopen('text.txt');
DATA=fscanf(fid,'%c');
fclose(fid);
  4 commentaires
Narges M
Narges M le 25 Juil 2013
E = textscan(f,'%s %f %f %f','delimiter', ' ', 'collectoutput',false,'BufSize',10000);
nl2605
nl2605 le 25 Juil 2013
tried this way too. But it gives E=0.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by