Function calling and executing from different .m files

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 Comments

Scripts are not meant for actual work, functions are a much better choice to do that. Since R2016b you can put functions in script files, so you can use them as local functions in your script. 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.
Your naming scheme is confusing. You have a function Return4 in a script file Return3, but the error suggests you have a function Return4 as well (which shouldn't even be called) containing at least this:
function Return4
% 34 lines go here
c_1 = ((mat_val_1) - ((mat_val_2*mat_val_3) / (mat_val_4 + mat_val_5))) * mat_val_6*mat_val_7
end
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
Also: do yourself and everybody who will ever touch your code a huge favor and don't use numbered variables. Use arrays instead. You may also consider structs, as they can have an informative description in the field name.
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.
@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.

Sign in to comment.

Answers (1)

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.

Products

Release

R2020b

Asked:

Jay
on 1 Oct 2020

Answered:

on 9 Oct 2020

Community Treasure Hunt

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

Start Hunting!