Search code examples
matlabcell-array

read multiple files in from a directoryusing matlab


How can read multiple files in from a directory using matlab? Can someone please help correct my code below:

files =dir(fullfile(directory_path,'*.dat'));
numfiles = length('*.dat');
mydat = cell(1, numfiles);

for k = 1:numfiles
    mydata{k} = fopen([directory_path,files(k).name]);
    values=textscan(mydata{k},'%s','delimiter','\n');
    fclose(fid);
    %fprintf(values)
    ....do something with values.....

end

.dat files are just many rows and single column of strings that need to be read in a loop and processed further.

Thanks


Solution

  • Thanks Zizy Archer.

    I solved the problem this way:

    files =dir(fullfile(directory_path,'*.dat'));
    numfiles = length(files);
    
    for k = 1:numfiles
        textFileName = [directory_path,files(k).name]
        fid = fopen(textFileName, 'r');
        textData = textscan(fid,'%s','delimiter','\n');
        fclose(fid);
    
        data = textData{:,1}
    end