How do you load m-files that contain filenames with integers and strings?

3 ビュー (過去 30 日間)
Brittny Freeman
Brittny Freeman 2021 年 5 月 18 日
編集済み: Stephen23 2021 年 5 月 18 日
I have a foler that containes multiple files with the following naming convention:
TTsim1Variable, TTsim2Variable, TTsim3Variable, TTsim4Variable....TTsim50Variable. Each "TT" files has the same number of rows and coloumns. Currently I am using the follwing code to load the data into my workspace:
B1 = load(sprintf('TTsim1Variable.mat'));
B2 = load(sprintf('TTsim2Variable.mat'));
B3 = load(sprintf('TTsim3Variable.mat'));
This is becoming quite tedious, is there a way to use the %d notation in the file names to make this process more efficent? Perhaps something like:
B1 = load(sprintf('TTsim%dVariable.mat'));

回答 (2 件)

the cyclist
the cyclist 2021 年 5 月 18 日
Use the %d notation, and a cell array for storing:
for ii = 1:50
B{ii} = load(sprintf('TTsim%dVariable.mat',ii));
end

Stephen23
Stephen23 2021 年 5 月 18 日
編集済み: Stephen23 2021 年 5 月 18 日
"...is there a way to use the %d notation in the file names to make this process more efficent?"
Of course, the documentation shows the basic concept:
For your files, something like this:
P = 'absolute or relative path to where the files are saved';
N = 50;
C = cell(1,N);
for k = 1:N
F = sprintf('TTsim%dVariable.mat',k);
C{k} = load(fullfile(P,F));
end
S = [C{:}]; % optional, if all files contain the same variables.

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by