Trying To Put Code In A File

2 Ansichten (letzte 30 Tage)
Patrick Powers
Patrick Powers am 23 Dez. 2018
Bearbeitet: madhan ravi am 23 Dez. 2018
I copied this directly from the Matlab documentation, put it in a file named "test", then typed the name of that file in the interpreter. Syntax error. Incorrect use of := operator.
myProc:= proc(n)
begin
if n = 1 or n = 0 then
1
else
n * myProc(n - 1)
end_if;
end_proc:
Now what?
  2 Kommentare
madhan ravi
madhan ravi am 23 Dez. 2018
share the link of the documentation where you took from
madhan ravi
madhan ravi am 23 Dez. 2018
Patrick Powers's answer moved here for consistency:
It's not online. It is in the Matlab documentation for "procedures."
I gave up on writing procedures and wrote a function instead, which worked.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

madhan ravi
madhan ravi am 23 Dez. 2018
Bearbeitet: madhan ravi am 23 Dez. 2018
Turns out that you are using MUPAD (to use it type MUPAD in command window) it's different from MATLAB so basically it's (your code) just a function , so if you want to use it in matlab then see https://www.mathworks.com/help/symbolic/convert-mupad-notebooks-to-matlab.html this converts the MUPAD file to valid MATLAB file.
Plus you know how simple it is in matlab , it's just
factorial(n)
But if you want to know the equivalent syntax for your code in MATLAB then:
a = myProc(10) % function call ,this part should be in a separate file or directly used in command window
function a = myProc(n) % function definition ,this part should be in a separate file named myProc.m
if n == 1 || n == 0
a = 1;
else
a = n * myProc(n - 1);
end
end
Note: See function to know how it works.

Kategorien

Mehr zu Get Started with MuPAD finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by