Load mat files more efficiently than if, elseif statement

I was given the code below for the purpose of loading a series of mat files, each containing the variables x1 and y1. This code is called in another code which defines xnew and events (which then loads the appropriate file) and performs calculations. The problem is that I have over 100 of these files, and the current setup would require each file to have it's own elseif statement, which is quite cumbersome. Is there a better way to do this that will still work with the other code?
function [tnew,vp,std_vp]=load_events(events,xnew)
if events==1
load Documents/event_picks/event1.mat
tnew=interp1(x1,y1,xnew,'cubic','extrap');
vp=0.425;
std_vp=std([0.32,0.33,0.35]);
elseif events==2
load Documents/event_picks/event2.mat
tnew=interp1(x1,y1,xnew,'cubic','extrap');
vp=0.384;
std_vp=std([0.32,0.33,0.35]);
elseif events==3
load Documents/event_picks/event3.mat
tnew=interp1(x1,y1,xnew,'cubic','extrap');
vp=0.769;
std_vp=std([0.32,0.33,0.35]);
end
%And the other code calls this
events=3; %pick event or events to analyze
xnew=1:2137;
[tnew,vp,std_vp]=load_events(events,xnew);

 Akzeptierte Antwort

Catalytic
Catalytic am 28 Jan. 2020
filename=fullfile('Documents/event_picks', "event"+events);
S=load(filename);
x1=S.x1;
y1=S.y1;

Weitere Antworten (0)

Kategorien

Mehr zu Variables finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2019b

Gefragt:

am 27 Jan. 2020

Beantwortet:

am 28 Jan. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by