Function calling and executing from different .m files
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2020-10-1
编辑:Stephen23
2020-10-1
@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.
回答(1 个)
Athul Prakash
2020-10-9
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!