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

3 views (last 30 days)
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 1x11 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.

Accepted Answer

KSSV
KSSV on 26 Mar 2017
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
  2 Comments
Ana Elena Francisco
Ana Elena Francisco on 26 Mar 2017
Hello KSSV,
Thank you for your help. I tried changing the code but it still does not work.
KSSV
KSSV on 26 Mar 2017
What error it shows? Post your code and error.

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!