필터 지우기
필터 지우기

call a function from another function with one changing argument

조회 수: 2 (최근 30일)
Ace_ventura
Ace_ventura 2015년 5월 4일
댓글: Ace_ventura 2015년 5월 4일
I have a function
function dos= beam(x)
This function calculates certain parameters such as "moment", "stress" etc. I have another function
function[c ce]=constraint(x,dos)
This function requires some parameters calculated in the first function such as "moment". The value of x must be same for both the functions. x is chosen randomly from a given defined set.for example
allx1_2=[1, 2 ,5 8 , 9 ,10];
I want that whatever the value is taken from the set for running the first function , the same value be passed to the second function. Both functions need to be in different scripts ( cannot be used in same script). I just need to know how will I provide function handles or any other way??

답변 (2개)

Walter Roberson
Walter Roberson 2015년 5월 4일
thisx = allx1_2(randperm(length(allx1_2),1); %select a random x
r1 = beam(thisx);
[r2, r3] = constraint(thisx, r1);
  댓글 수: 1
Ace_ventura
Ace_ventura 2015년 5월 4일
편집: Ace_ventura 2015년 5월 4일
Thank you Walter but this is how I have exactly defined my function.To be more clear to you , I have a third function defined as x=random(x). So overall , I have three functions defined in separate scripts.Now, any x value taken from random (x) function should be passed to beam(x) function in different script and then this beam(x) function would calculate "moment" . Then I have to pass same x value to third script constraint(x,dos) because I have to use same x value and the "moment value "calculated in the second function Function dos=beam(x)..Again, all three are in different scripts. I hope you understand what i am trying to ask. If there is any confusion please do ask:). I just want to know how to pass values.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2015년 5월 4일
편집: Stephen23 2015년 5월 4일
The easiest, neatest and most bug-free way of passing values is to ... pass variables!
Although beginners seem to love writing scripts, learn to write functions rather than scripts and then passing variables is easy to write, easy to debug and easy to follow the information flow with. Functions also have many other advantages including their own workspaces, and nice things like that.
MATLAB themselves have documented how to pass values between workspaces, the preferred method is to pass variables:
It can be as easy as creating a meta-function that calls several other functions, and then your entire question can be resolved by something like this:
function my_meta_fun(x)
dos = beam(x);
[c,ce] = constraint(x,dos);
... other code

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by