Octave: Load all files from specific directory
It's usually bad design if you load files to variables which name is generated dynamically and you should load them to a cell array instead but this should work:
files = glob('C:\folder\*.txt')
for i=1:numel(files)
[~, name] = fileparts (files{i});
eval(sprintf('%s = load("%s", "-ascii");', name, files{i}));
endfor
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I used to have Matlab and loaded all txt-files from directory "C:\folder\" into Matlab with the following code:
myFolder = 'C:\folder\'; filepattern = fullfile(myFolder, '*.txt'); files = dir(filepattern); for i=1:length(files) eval(['load ' myFolder,files(i).name ' -ascii']); end
If C:\folder\ contains A.txt, B.txt, C.txt, I would then have matrices A, B and C in the workspace.
The code doesn't work in octave, maybe because of "fullfile"?. Anyway, with the following code I get matrices with the names C__folder_A, C__folder_B, C__folder_C. However, I need matrices called A, B, C.
myFolder = 'C:\folder\'; files = dir(myFolder); for i=3:length(files) eval(['load ' myFolder,files(i).name ' -ascii']); end
Can you help me? Thanks, Martin
PS: The loop starts with 3 because files(1).name = . and files(2).name = ..
EDIT: I have just found a solution. It's not elegant, but it works. I just add the path in which the files are with "addpath", then I don't have to give the full name of the directory in the loop.
myFolder = 'C:\folder\'; addpath(myFolder) files = dir(myFolder); for i=3:length(files) eval(['load ' files(i).name ' -ascii']); end