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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!