MATLAB: loading files from a folder by extension

What is the easiest way to download all files from a folder with the same extension in MATLAB?

Previous solutions from me:

%%% Will load a file if its filename is provided %%% USAGE: (Best save data to a variable to work with it.) %%% >> x = loadwrapper('<file_name>') %%% ... and then use 'x' all the way you want. %%% <file_name> works with absolute and relative paths, too. function [ loaded_data ] = loadwrapper( file_name ) files = dir(file_name); loaded_data = load(files.name); end 

and

 %%% put this in a new script, in a function it WILL NOT WORK! %%% and fix your paths, ofc. i left mine in here on purpose. %%% SETTINGS folderName='/home/user/folder/'; extension='*.dat'; %%% CODE concattedString=strcat(folderName, extension); fileSet=dir(concattedString); % loop from 1 through to the amount of rows for i = 1:length(fileSet) % load file with absolute path, % the fileSet provides just the single filename load (strcat(folderName, fileSet(i).name)); end %%% TIDY UP %%% only imported files shall stay in workspace area clear folderName; clear extension; clear concattedString; clear fileSet; clear i; 
+5
source share
3 answers

You can use dir to get all the files you want. Then you can iterate over the for loop and call load for each. For example, the following:

 files = dir('C:\myfolder\*.txt'); for k = 1:length(files) load(files(k).name, '-ascii') end 

uploads all files to "C: \ myfolder" with the extension "txt".

+7
source

If you want to import all functions from a directory, you can use addpath:

In Matlab, you are in the c: \ matlab \ work directory, and you click:

 addpath directory_where_all_my_functions_are 

to import all functions c:\matlab\work\directory_where_all_my_function_are

help addpath in matlab for more info

0
source

** Not an answer, but a continuation. It was not allowed to comment, but there is a comment / question that is very related.

In my code, I want the user to manually select the .wav file folder for processing. I used:

 dname=uigetdir('C:'); 

dname gives the path to the folder directory and saves it as a variable

I know that you can use the directory name cd and cd .. as in linux with MATLAB, how do I disconnect the meaningful part of dname in order to be able to use the cd function?

-1
source

All Articles