Calling a variable using a function - basic
조회 수: 3 (최근 30일)
이전 댓글 표시
How do I call a function from one script to another?
Script / .m file 1:
File name: Return1 (the variable that is to be called)
function c = rand_val
rand_val = rand
end
Script / .m file 2:
File name: Main (script calling the variable)
function c = Return1(rand_val)
c = rand_val()
end
And can someone please tell me (or direct me to where I can get the information regarding) to breakdown what the commands are envoking?
I am basing the above code of other posts in the community but it appears even those are too advanced for me currently.
Thanks in advance.
댓글 수: 0
답변 (2개)
madhan ravi
2020년 9월 30일
Return1 % call it
function c1 = rand_val
c1 = rand;
end
function c = Return1
c = rand_val;
end
댓글 수: 0
Steven Lord
2020년 9월 30일
function c = rand_val
rand_val = rand
end
When you call this function it will throw an error. You'd defined rand_val to return a variable c, but you never create the variable c inside this function. As madhan ravi suggested, try changing the second line so it defines a variable c instead of a variable rand_val and see if that works better.
function c = Return1(rand_val)
c = rand_val()
end
As written this function accepts a variable, indexes into it with no indices, and returns the result of that indexing as the output of the function. But you don't want it to index into a variable, you want it to call the rand_val function, right? Eliminate the input argument (and modify the places where you call it to call it without any input arguments.)
function c = Return1()
c = rand_val()
end
I am basing the above code of other posts in the community but it appears even those are too advanced for me currently.
In that case, you might find the free MATLAB Onramp tutorial useful. It is intended to teach the basics of working with MATLAB.
댓글 수: 2
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!