Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB function syntax [closed]

Tags:

matlab

I'm trying to learn MATLAB now; I tried a simple step, factorial function.

factorial.m:

function result = factorial (m)
    if m == 1
        result = m;
    else
       result = m .* factorial(m.-1);
    end

and then call it like this:

x = 2;
f = factorial (x)

but all I get is an error:

Missing variable or function.

like image 740
Katia Avatar asked Apr 05 '11 07:04

Katia


People also ask

What does close do in MATLAB?

close all closes all figures whose handles are visible. A figure handle is hidden if the HandleVisibility property is set to 'callback' or 'off' . example. close all hidden closes all figures, including figures with hidden handles.

What does close mean in MATLAB?

close deletes the current figure (equivalent to close(gcf) ). close(h) deletes the figure identified by h. If h is a vector or matrix, close deletes all figures identified by h. close name deletes the figure with the specified name.

How do I close all open scripts in MATLAB?

fclose('all') closes all open files. status = fclose(___) returns a status of 0 when the close operation is successful. Otherwise, it returns -1 . You can use this syntax with any of the input arguments of the previous syntaxes.


1 Answers

  1. You have a syntax error, in the second line there shouldn't be a . after the 2nd m
  2. The if should be in a separate line from the function declaration.
like image 160
Itamar Katz Avatar answered Oct 26 '22 21:10

Itamar Katz