How do I change File Name Variable within a Loop

88 views (last 30 days)
Looked at sprintf() and did not understand it. Keep it simple, I do not understand code, this is for a uni assignment, prof expects us to "just google it" when we run into issues
Trying to load 12 files. Names are "Ss_R_L.mat" where
s is a number between 1 and 3
R is either "Walk" or "Run"
L is either "90PSL" or "PSL"
for R = 1:2
if R == 1
% use "Walk"
else
% use "Run"
end
for L = 1:2
if L == 1
% use "PSL"
else
% use "90PSL"
end
for S = 1:3
if S == 1
load('S1_R_L.mat')
elseif S == 2
load('S2_R_L.mat')
else
load('S3_R_L.mat')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Plotting Data from the Files %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
end
end

Accepted Answer

Hiroki Okawa
Hiroki Okawa on 21 Feb 2020
for R = 1:2
if R == 1
txt_R = 'Walk';
else
txt_R = 'Run';
end
for L = 1:2
if L == 1
txt_L = 'PSL';
else
txt_L = '90PSL';
end
for S = 1:3
filename = ['S', num2str(S), '_', txt_R, '_', txt_L, '.mat']
load(filename)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Plotting Data from the Files %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
end
end
or
Rs = {'Walk', 'Run'};
Ls = {'PSL', '90PSL'};
for r = 1 : 2
for l = 1 : 2
for s = 1 : 3
filename = ['S', num2str(s), '_', Rs{r}, '_', Ls{l}, '.mat']
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Plotting Data from the Files %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
end
end

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!