Taking input data from multiple files

Hey everyone I have multiple files by the name of outputfile0001.txt, outputfile0002.txt ... upto outputfile0060.txt . each file has a structure like this:
  1. 0.26630448E-08 0.62768422E+00
  2. 0.30589992E-08 0.64235565E+00
  3. 0.35138231E-08 0.65737000E+00
  4. 0.40362683E-08 0.67273529E+00
  5. . .
  6. . .and so on.basically two sets of numeric arrays gi(i) and rri(i)are there in these files.Now what i want is to is from each file plot(rri(),gi()).Any help is appreciated.I tried the code below with error that:'error in opening file'
numfiles = 60; mydata = cell(1, numfiles);
for k = 1:numfiles myfilename = sprintf('outputfinal%d.txt', k); mydata{k} = importdata(myfilename); end

Risposte (1)

myfilename = sprintf('outputfinal%d.txt', k)
will produce e.g.
outputfinal1.txt
To get the zeros in the filename, use
myfilename = sprintf('outputfinal%04d.txt',k)
Best regards, Michael

6 Commenti

Bhaskar
Bhaskar il 23 Set 2014
Modificato: Bhaskar il 23 Set 2014
Still getting the error:(hfhfhf ) is name of .m file in the same directory of text files.
Error using importdata (line 137)
Unable to open file.
Error in hfhfhf (line 6)
mydata{k} = importdata(myfilename);
Could you please display the content of myfilename by just removing the ";" after the sprintf command? Does it match with the respective file name in your folder?
Also, sometimes single files are missing? Does the error occur at the very first loop iteration or somewhere later? You can enter
dbstop if error
in the command window to stop the program right before failing. Then, you can easily check the values of k and myfilename.
Yes it occurs in the first loop. I changed the code to this:
if true
% numfiles = 2;
mydata = cell(1, numfiles);
pathName='D:\Atmospheric and cloud engineering\cloud spectrum data\FINAL RESULTS\A BRAVE NEW WORLD';
for k = 1:numfiles
myfilename = sprintf('r%.4d.txt', k);
fullNameWithPath = fullfile(pathName, myfilename);
mydata{k} = importdata(fullNameWithPath);
end
end
value of fullNameWithPath is :D:\Atmospheric and cloud engineering\cloud spectrum data\FINAL RESULTS\A BRAVE NEW WORLD\r0001.txt
k=1
And the file is actually there. So still clueless about the problem.
If you open the file via
fid=fopen(fullNameWithPath)
what value does fid have?
for fid it shows:
Undefined function or variable 'fid'.
By the way i should mention that i am getting txt files from a fortran 77 program.
I mean, if you write the code line I have provided in the previous comment in your loop, before the importdata line. Then fid will have a value. -1 indicates that it cannot find the file, any other value (>2) indicates that the file can be accessed. Instead, you could also write
dir(fullNameWithPath)
to check if Matlab really finds the file.

Accedi per commentare.

Prodotti

Richiesto:

il 23 Set 2014

Community Treasure Hunt

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

Start Hunting!

Translated by