Function calling and executing from different .m files

21 Ansichten (letzte 30 Tage)
Jay
Jay am 1 Okt. 2020
Beantwortet: Athul Prakash am 9 Okt. 2020
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 "
Thanks.
  3 Kommentare
Jay
Jay am 1 Okt. 2020
Thanks for the repy Rik.
The 34 lines are just comments trying to understand the requirements and aren't any commands or variables.
I don't understand what you mean by: Those functions work the same as local functions in function .m files: you can't call them directly. You can only call the main function.
Are you saying that the function.m files need to be called and the entire function file run rather than invoking a function in another script?
I'll try to remember the naming convention of variables shouldn't be alphanumeric.
I wrote another post asking how I reference cells of an array when specifiying a function as it's neater and just received a solution.
Thankyou for the constructive feedback.
Stephen23
Stephen23 am 1 Okt. 2020
Bearbeitet: Stephen23 am 1 Okt. 2020
@Jay: your code is confusing:
  • you defined function Return4 at the end of script1, but you never call that function so none of the code in the function is ever executed.
  • your write that you have a second "Script 2 file name: Return4" but it appears to be actually a function named c_1_out:
function [c_1] = c_1_out
Local functions (including those defined in scripts) cannot be called directly outside of the file where they are defined. In general you can only call the main function (whose name should be the same as the filename):
"I'll try to remember the naming convention of variables shouldn't be alphanumeric."
The problem is not specifically with alphanumeric variable names. The problem is that some beginners like to number their variables x1, x2, x3, ..., which forces them into writing slow, complex, inefficient, buggy code when they try to access their data. The simple and efficient alternative is to use actual indexing into one array rather than pseudo-indexing in variable names.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Athul Prakash
Athul Prakash am 9 Okt. 2020
Hi Jay,
As an alternative, you may try saving the initialized matrix to disk (typucally as a .mat file) and loading it in the script which requires the data.
Hope it Helps!
Additionally, you may try the Matlab OnRamp course online. In my experience, it's a good starting point to cover Matlab coding foundations well as you start picking up the language.

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by