When I run the following code it only shows the last file in the directory in MATLAB workspace? How do I get all the files in my directory saved in the workspace?

1 view (last 30 days)
ivsFiles = dir('*.ivs');
numfiles = length(ivsFiles);
for k = 1:103;
filename = ivsFiles(k).name;
fileID = filename;
C = textscan(filename,'%d');
end
  3 Comments
Anuradha Bhattacharya
Anuradha Bhattacharya on 22 Sep 2016
I tried this code:
ivsFiles = dir('*.ivs');
numfiles = length(ivsFiles);
for k = 1:103;
filename = ivsFiles(k).name;
fileID = filename;
fid = fopen('ra%d.ivs',k);
C = textscan(fid,'%d');
fclose(fid);
end
It is giving error:
Error using fopen Invalid permission.
My files do not have any password settings on them.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 22 Sep 2016
Edited: KSSV on 22 Sep 2016
ivsFiles = dir('*.ivs');
numfiles = length(ivsFiles);
C = cell(numfiles,1) ; % initialize each file's data into a cell
for k = 1:numfiles;
filename = ivsFiles(k).name;
fileID = filename;
C{i} = textscan(filename,'%d');
end
Please note that, you will eat up the memory if the files are huge.
  2 Comments
Walter Roberson
Walter Roberson on 22 Sep 2016
ivsFiles = dir('*.ivs');
numfiles = length(ivsFiles);
C = cell(numfiles,1) ; % initialize each file's data into a cell
for k = 1:numfiles;
filename = ivsFiles(k).name;
fileID = fopen(filename, 'rt');
C{i} = textscan(fileID,'%d');
fclose(fileID);
end

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!