Load mat files more efficiently than if, elseif statement

2 ビュー (過去 30 日間)
Sam Hone
Sam Hone 2020 年 1 月 27 日
回答済み: Catalytic 2020 年 1 月 28 日
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);

採用された回答

Catalytic
Catalytic 2020 年 1 月 28 日
filename=fullfile('Documents/event_picks', "event"+events);
S=load(filename);
x1=S.x1;
y1=S.y1;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by