Choose a function using a switch statement.

4 ビュー (過去 30 日間)
jo brown
jo brown 2015 年 4 月 4 日
コメント済み: John D'Errico 2015 年 4 月 5 日
Hi, I am trying to create a switch function that will allow the user to choose to run one of two functions. Here is what I have :
function stressstrain
result = ...
input('For Plane Stress press 1, for Plane Strain press 2 and then press enter \n');
switch result
case 1
case 2
otherwise disp
('You pressed an unspecified key, please run again')
end
end
Is it possible to call and run a function using the case command ?
Apologies if this is a poorly worded and trivial question, I have only limited experience of matlab, but have an assignment due in very soon ! Thanks, Joshua
  2 件のコメント
per isakson
per isakson 2015 年 4 月 4 日
編集済み: per isakson 2015 年 4 月 4 日
Yes! Did you try?
jo brown
jo brown 2015 年 4 月 5 日
Yes, I tried ! But I'm a novice and have very limited experience of matlab.

サインインしてコメントする。

回答 (2 件)

John D'Errico
John D'Errico 2015 年 4 月 4 日
編集済み: John D'Errico 2015 年 4 月 4 日
It looks like you are almost there, so here is an example of a switch construct that will call two different functions randomly.
switch round(rand(1))
case 0
x = cos(57);
case 1
x = sin(eps);
otherwise
% of course, we can't get into this otherwise branch
% given the way I generated the switch argument, so I
% could have left it out completely.
disp('the sky is falling!')
end
  1 件のコメント
jo brown
jo brown 2015 年 4 月 5 日
Nope, I don't get it :(
I have two variations of a function that I need to allow a user to call. Specifically, its the syntax after the case statement that I am not sure of.
I'll keep trying, but would appreciate any further help.

サインインしてコメントする。


jo brown
jo brown 2015 年 4 月 5 日
編集済み: John D'Errico 2015 年 4 月 5 日
I think I got it ! How does this look ?
function stressstrain
result = input('For Plane Stress press 1, for Plane Strain press 2 and then press enter \n');
switch result
case 1
run PStress
case 2
run PStrain
otherwise disp ('You pressed an unspecified key, please run again')
end
  3 件のコメント
jo brown
jo brown 2015 年 4 月 5 日
Hey John, thanks for your input. It seems to work either way, but I guess less code is better.
John D'Errico
John D'Errico 2015 年 4 月 5 日
Yep. Run works there, and will give the identical results, with or without. So I suppose I should test to see if there is a time cost for using run. Does run add extra overhead? Let us see...
I've created a simple script, that does essentially nothing interesting.
type testscript
a = 2;
b = a+3;
tic
for i = 1:100000
testscript
end
toc
Elapsed time is 1.357657 seconds.
So 100000 calls to testscript took a bit over 1 second. Now I'll re-run the test using run in there.
tic
for i = 1:100000
run testscript
end
toc
Elapsed time is 167.432911 seconds.
So there IS a significant cost to the use of run. Not truly massive per execution, taking roughly an extra 0.0017 seconds per execution of run.
(167.43 - 1.36)/100000
ans =
0.0016607
A few CPU cycles here, a few there, and eventually it still all adds up to not that much, unless you will be using run a few millions of times.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStress and Strain についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by