Load mat files more efficiently than if, elseif statement

2 visualizzazioni (ultimi 30 giorni)
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);

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by