MATLAB: How to import text files to a cell array from a folder

import filesMATLAB

Hello,
I know this might be a popular question, but I've tried the proposed solutions in other questions and I couldn't make my code work.
My problem is the following: I have .txt files with electrophysiology recordings. Each file has headerlines, followed by the data (3 column). I have a function that works perfectly opening single files, but I would like to write one that could open all the files in a given folder, and that imports the information to a cell array.
This is my current code:
function mydata = openFolder(x)
C = dir(x);
C = C(~[C.isdir]);
numfiles = numel(C);
mydata = cell(1, numfiles);
for ii = 1:numel(C)
fid = fopen(C(ii).name, 'w+');
mydata{ii} = textscan(fid,'%s %s %s', 'headerlines',4);
fclose(fid);
end
The code is able to read the files (the fid number is high enough), and mydata cell seems to have the right size (if I am loading a folder with 11 .txt files, mydate is a 1×11 cell array). But when I explore one of the cell arrays contained in myfolder variable, they are empty. There is no data inside.
I would appreciate any help.

Best Answer

dinfo = dir('*.txt');
for K = 1 : length(dinfo)
thisfilename = dinfo(K).name; %just the name
%read the file data
%do something with the data
end
Related Question