Calling a variable using a function - basic

4 vues (au cours des 30 derniers jours)
Jay
Jay le 30 Sep 2020
Commenté : Rik le 1 Oct 2020
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.

Réponses (2)

madhan ravi
madhan ravi le 30 Sep 2020
Return1 % call it
function c1 = rand_val
c1 = rand;
end
function c = Return1
c = rand_val;
end

Steven Lord
Steven Lord le 30 Sep 2020
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 commentaires
Jay
Jay le 30 Sep 2020
Modifié(e) : Jay le 1 Oct 2020
The above script works when a random value is generated but will not work when the function needs to reference the different values in a matrix.
I realise in hindsight I should have given a better example that better represents the code I am working with (not expect a simplified question would address the requirements).
My mistake and I appologise for that.
Please see a better representation below.
Script 1 file name: Return3
Purpose: To have initial values in a matrix that will be used in another script file containing a function.
Said function will use the matrix values in the cell as referenced and have a single output value.
% Create a 1 x 7 matrix with random variables - intialise
mat_val(1,7) = zeros
% input different values in each cell of matrix
for i = 1:7
j = rand
mat_val (1,i) = j
end
mat_val_1 = mat_val(1,1)
mat_val_2 = mat_val(1,2)
mat_val_3 = mat_val(1,3)
mat_val_4 = mat_val(1,4)
mat_val_5 = mat_val(1,5)
mat_val_6 = mat_val(1,6)
mat_val_7 = mat_val(1,7)
% format is first term is output and the second term is the file and input
function c_1_out = Return4(c_1)
c_1_out = Return4(mat_val_1, mat_val_2, mat_val_3, mat_val_4, mat_val_5, mat_val_6, mat_val_7
end
Script 2 file name: Return4
Purpose: To read in the single value from the script named file named Return1
function [c_1] = c_1_out
% Use values in the mat_val cells as stated in the formula and output
% single value
c_1 = ((mat_val_1) - ((mat_val_2*mat_val_3) / (mat_val_4 + mat_val_5))) * mat_val_6*mat_val_7
end
The following error is returned:
" Unrecognized function or variable 'mat_val_1'.
Error in Return4 (line 34)
c_1 = ((mat_val_1) - ((mat_val_2*mat_val_3) / (mat_val_4 + mat_val_5))) * mat_val_6*mat_val_7 "
Can someone please explain where I am going wrong and why like Steven did in his last post?
I marked this as unsolved as to not add another post on 'Functions', I hope that's the correct way to address questions that are better elaborated at a later date (if not, please inform me to create a new post or other community expected methods for a resolution).
I have also compelted the onramp with ease (7. Calling Functions) but I think this exceeds it's scope as per Steve's suggestion.
Thanks.
Rik
Rik le 1 Oct 2020
[note that Jay has now posted this as a separate question]

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by