Function calling and executing from different .m files
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
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.
Antworten (1)
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!