필터 지우기
필터 지우기

Load mat files more efficiently than if, elseif statement

조회 수: 1 (최근 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 CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by