How do I change File Name Variable within a Loop

94 次查看(过去 30 天)
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

采纳的回答

Hiroki Okawa
Hiroki Okawa 2020-2-21
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Google 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by