Load mat files more efficiently than if, elseif statement

2 views (last 30 days)
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);

Accepted Answer

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

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!