How to load multiple files and save each as a matrix?

6 views (last 30 days)
Here is my current code:
myFolder = uigetdir('*.txt', 'Pick a magnetic calibration file to work with');
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.txt');
txtFiles = dir(filePattern);
n=length(txtFiles);
for k = 1:n;
baseFileName = txtFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
sprintf('%s\n', fullFileName);
A = textread(fullFileName);
end
I am trying to load an entire folder's worth of files and save them all as matrices. Everything works great as far as calling on the folder, but can't get all the files to save. As it is I am only left with A= the last file that is loaded. I know I need another loop in there somewhere but I just can't get it. Suggestions?

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 5 Dec 2014
A = textread(fullFileName);
this will erase the previous value of A after each loop, you can use a cell array to save them all
A{k} = textread(fullFileName);

Categories

Find more on File Operations 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!