Matlab Path Generation

back to Fan's Reusable Matlab Repository or Dynamic Asset Repository.

Contents

Get all files from a folder

% Generate Folder String
root = 'c:';
user = 'Users/fan';
project = 'M4Econ';
subfolder = 'support/string/';
folder = fullfile(root, user, project, subfolder);

% get all files in Folder
sc_files = dir(folder);

% display
disp(sc_files)
sc_files.name
  5x1 struct array with fields:

    name
    folder
    date
    bytes
    isdir
    datenum


ans =

    '.'


ans =

    '..'


ans =

    'fs_string.m'


ans =

    'fs_string_strcat.m'


ans =

    'html'

Get all m files from a folder

% Generate Folder String
root = 'c:';
user = 'Users/fan';
project = 'M4Econ';
subfolder = 'support/string/';
filename = '*.m';
folder = fullfile(root, user, project, subfolder, filename);

% get all files in Folder
sc_files = dir(folder);

% display
disp(sc_files)
sc_files.name

% display
for it_ctr = 1:length(sc_files)
    disp(sc_files(it_ctr).name);
end
  2x1 struct array with fields:

    name
    folder
    date
    bytes
    isdir
    datenum


ans =

    'fs_string.m'


ans =

    'fs_string_strcat.m'

fs_string.m
fs_string_strcat.m

One Line Gen Dir if Does not Exist

% Generate Folder String
root = 'c:';
user = 'Users/fan/';
project = '/M4Econ/';
subfolder = 'support////string';
folder = fullfile(root, user, project, subfolder);

% Generate folder if does not already exist
if ~exist(folder,'dir'); mkdir(folder); end