How To Read Procedure From File?

I've written a procedure and saved it in a file. It passes the syntax text. Now I want to be able to call that procedure from Matlab.
>> run( vecrot3 )
Attempt to execute SCRIPT vecrot3 as a function:
C:\Users\Lenovo\Documents\Matlab Scripts\vecrot3.m
Matlab is correct in that this is what I am trying to do. What's the magic word?

5 Comments

The argument needs to be a string, not the function; ML is trying to execute it first and then pass its result to run.
You don't need run at all, just write
vecrot3
to execute the script.
If you really, really, wanted to use run for some unknown reason (about only real one is if it isn't on the path, but that doesn't seem to be the case or you'd have gotten the "no such function" error instead, that syntax would be
run('vecrot3')
to pass the name.
That didn't work either. The problem appears to have been that the name of the function was the same as the name of the file. So I changed the file name so that they are different. Now I get a new error when I type the name of the new file. The error is that the function vecrot3 is undefined. That is not surprising, since vecrot3 is the function I am trying to define.
I doesn't help that I don't know the meaning of the := operator. I'm operating on monkey see, monkey do.
vecrot3 := proc( v, th )
local M;
begin
M = [ cos(th) -sin(th) 0 ;
sin(th) cos(th) 0;
0 0 1
];
(v * M) ;
end_proc;
madhan ravi
madhan ravi on 23 Dec 2018
Edited: madhan ravi on 23 Dec 2018
that doesn‘t look like a valid Matlab syntax , upload the files to test
The Code Analyzer Report seems to think it is OK.
Care to share just why you think the syntax is wrong?
"Care to share just why you think the syntax is wrong?"
For a start, this line:
vecrot3 := proc( v, th )
The MathWorks makes many different tools, e.g. MATLAB, Simulink, etc.. Some of those tools can work together, but they are still separate tools. That line shows that you are reading/copying some MuPad code. If you want to use the MuPad tool, then that is perfect.
If you think that you are writing MATLAB code, then this will not help you, in exactly the same way that reading Java documentation will not help you to write Haskell code.
You need to be clear what tools you are using and follow the appropriate documentation.
The best place to learn basic MATLAB concepts and learn what MATLAB code looks like, is in the introductory tutorials:

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 23 Dec 2018
Edited: madhan ravi on 23 Dec 2018
ADDENDUM to dpb's comment :
"Care to share just why you think the syntax is wrong?"
v = 1:3; % an example
th = pi;
Result = vecrot3( v, th ) % function call
function Result = vecrot3( v, th ) % function definition
M = [ cos(th) -sin(th) 0 ;
sin(th) cos(th) 0 ;
0 0 1 ];
Result = v * M ;
end
Note: Beware MUPAD is going to be removed in the future release so get used to MATLAB as soon as possible.

Tags

Asked:

on 23 Dec 2018

Edited:

on 23 Dec 2018

Community Treasure Hunt

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

Start Hunting!