how to call a function one m-file to another m-file
조회 수: 1 (최근 30일)
이전 댓글 표시
lets say i have one m-file named call1.m having:
function t =call1(a,b)
a=10;
b=5;
t=a+b;
end
and i want to call and run this function 't' in another m-file named call2.m, i wrote it as:
function call2()
t = @call1
end
but nothing happens please guide me.
Thanks
댓글 수: 0
답변 (1개)
Star Strider
2015년 10월 29일
You only need the ‘@’ operator (creating a function handle) if you are using the function as an input argument to another function. You are not here, so do this instead:
function call2()
t = call1
end
댓글 수: 1
Steven Lord
2015년 10월 29일
Star Strider's suggestion will work because of the way your call1 function is written. The line that declares call1 tells MATLAB that it accepts up to 2 input arguments. Star Strider's code calls it with 0, and that works, because call1 completely ignores what you pass into it and overwrites those variables with hard-coded data.
If you eliminate the lines in call1 that assign values to a and b, then you could do this using a slightly modified version of Star Strider's code:
function call2()
t = call1(10, 5)
end
If you want to DO anything with that result, though, you probably want to define call2 in such a way that it returns the variable t to its caller.
function t = call2()
t = call1(10, 5);
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Pulsed Waveforms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!