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

조회 수: 5 (최근 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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by