0

I have a folder with multiple textfiles (labelled xxxxxds.text where x's are numbers) with data in it. The data in the text file is arranged in 3 columns and the first line of a text file consists of headers, one for each column. What I want to do is, to open the folder and read the text file # 1 say and do some processing like delete the first line and store the rest of data in three separate variables (one for each columns), then move to the second file and do the same thing and keep doing it until the end file is reached.

I am not having too much success with it. Here is my (failed) attempt at it:

dir_folder ='D:\datat_folder';

files = dir(dir_folder);

files = files(arrayfun(@(x) ~strcmp(x.name(1),'.'),files)); % Remove hidden files


dir_length=length(files);


for steps=1:dir_length % for loop



  point=[files,num2str(steps),'*.txt']



    [A,B,C] = textread(point,'%f %f %f' , 2);


end

I would be grateful for suggestions if anyone has any.

Cheers.

2
  • What is the use of the inner for loop? Commented Nov 22, 2014 at 21:13
  • Sorry I think I wasnt very careful when making the post. You are right the inner for loop shouldn't be there. Edited the post. Commented Nov 23, 2014 at 11:57

1 Answer 1

1

To get just the filenames that you want, you can be more specific with your dir command:

dir_folder ='D:\datat_folder\';  %note the '\' at the end
fnames = dir([dir_folder '*.text'); 

Then, loop over each file, as you were doing, but alter your textread command a little bit:

for Ifile = 1:length(fnames)
    fname = fnames(Ifile).name;

    %load the text data while using the 'headerlines' option to ignore the first line of data
    [A,B,C]=textread(fname,'%f %f %f','headerlines',1);

    %do your processing here
    % blah blah blah

end
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it worked. I was not defining the path to the directory correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.