how to load .m funtion to my code?
4 views (last 30 days)
Show older comments
Hey,
I have a simple question.
when I use functions in C , I need to write the declarations above the main and the functions with their content I write below the main.
In my homework we need to write the functions as a new blablabla.m file.
I noticed that when I am writing a code, assume the file name is question.m , so I actually need to drag the function.m file into the "current folder" window in matlab - but only if I actually drag it!
I dont undersrand why, since they are in the same folder so why it cant recognize it?
So, my question is actually , how can I ensure that the person who will check my hw will load it when he checks the code? is there a way to load it by a command , if it is in the same folder? because I tried the "load" command while the code and the function had been in the same folder and it didnt work.
it worked only when I dragged it.
Thank you :)
0 Comments
Answers (1)
Steven Lord
on 13 May 2020
I'm not 100% certain I understand the problem you're experiencing, but my guess is that you have a script file foo that defines some variables
% This is foo.m
x = 1;
y = 2;
z = 42;
and a script or function baz that uses those variables.
% baz.m
w = x + y*z;
If this is the case, you need to run foo before running baz. One way to do this is to have baz itself run foo.
% baz.m (modified)
foo
w = x + y*z;
If baz is a function:
function w = baz
w = x + y*z;
you'd need to create the variables inside the function itself:
function w = baz
foo
w = x + y*z;
or you need to tell the user of baz to run the script and pass the variables into baz.
function w = baz(x, y, z)
% To run baz.m you need to create x, y, and z
% One way to create these variables is to run the script foo
%
% Example:
%
% foo
% w = baz(x, y, z)
w = x + y*z;
0 Comments
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!