Switch case option valid for two cases?

조회 수: 1 (최근 30일)
Rhiannon Vieceli
Rhiannon Vieceli 2019년 7월 31일
편집: dpb 2019년 8월 1일
Hi, there.
Thanks in advance for any help!
I'm trying to extract P and/or S wave velocity profiles from corresponding text files. Here is what I'm trying. It doesn't seem to like that 'ps' is being called for both cases.
I know I could just copy the separate 'p' and 's' case options into a third 'ps' option, but I was hoping there was a way to not do that.
n = input('Which velocity model(s)? Enter p, s, or ps in single quotes: ');
switch n
case {'p','ps'}
% code to extract P velocity from P velocity text file
case {'s','ps'}
% code to extract S velocity from S velocity text file
otherwise
warning('Invalid choice')
end

채택된 답변

dpb
dpb 2019년 7월 31일
편집: dpb 2019년 8월 1일
But, presuming 'ps' means to do both 'p' and 's', you need that case as well as written anyway because in Matlab the switch block case statements do NOT "fall through" as in C.
opt=input('Which velocity model(s)? Enter p, s, or ps: ','s'); % return string
if contains(opt,'p')
% code to extract P velocity from P velocity text file
end
if contains(opt,'s')
% code to extract S velocity from S velocity text file
end
does either or both and is also insensitive to your user entering 'sp' instead of 'ps'. Of course, it will also do both if user enters 'stop' so should probably do some input verification as well.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Verification, Validation, and Test에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by