0

Here is my code that i tried to write a matlab function that takes a matrix as input and returned a matrix as output.

a=[ 1 2 3; 4 5 6; 7 8 9];
function [s]= try_1(a)
  %takes a matrix as a input
   display(' data matrix');
   disp(a);
   disp('dimension of the matrix');
   [m n]= size(a); %calculate the dimension of data matrix
   s = mean(a);
end
meskat Tanu
  • 11
  • 1
  • 6

2 Answers2

3

You cannnot define a function inside a script. MATLAB assumes your file is a script, because it starts with a=[ 1 2 3; 4 5 6; 7 8 9]; - i.e. by defining a variable. MATLAB thus assumes a series of instructions is following and throws an error when it sees the function definition.

You also have to distinguish function definitions and function calls. With your code above, i.e.

function s = try_1(a)
...
end

you define what the function does (function definition), but you do not call it yet, i.e. nothing happens. For something to happen, you will have to call it by

a=[ 1 2 3; 4 5 6; 7 8 9];
s = try_1(a);

in a script or in the workspace.

About file names and what to put in each file: Functions are identified by their filename in MATLAB. It is absolutely necessary to have the function try_1() in a file called try_1.m. And this file can not contain anything else. The a=[ 1 2 3; 4 5 6; 7 8 9]; and function calls belong in a separate script (or to test the behavior simply type it in the command window).

hbaderts
  • 14,136
  • 4
  • 41
  • 48
0

Explain clearly where you execute the above code ? if you execute in command prompt don't use function. code likes this

  a=[ 1 2 3; 4 5 6; 7 8 9];
  %takes a matrix as a input
   display(' data matrix');
   disp(a);
   disp('dimension of the matrix');
   [m n]= size(a); %calculate the dimension of data matrix
   s = mean(a);

or if you are not using command prompt then put the value "a" in another function and call the try_1 function from new function you created. code like this

function parent()
a=[ 1 2 3; 4 5 6; 7 8 9];
s= try_1(a)
end  

function [s]= try_1(a)
      %takes a matrix as a input
       display(' data matrix');
       disp(a);
       disp('dimension of the matrix');
       [m n]= size(a); %calculate the dimension of data matrix
       s = mean(a);
end

or assign then value inside the try_1 function . code like this

function [s]= try_1(a)
a=[ 1 2 3; 4 5 6; 7 8 9];
  %takes a matrix as a input
   display(' data matrix');
   disp(a);
   disp('dimension of the matrix');
   [m n]= size(a); %calculate the dimension of data matrix
   s = mean(a);
end
Karthick Rajan
  • 394
  • 2
  • 17
  • It would actually be nice if you explained **why** the OP is getting the error. All you're doing is telling them what they should do instead. – rayryeng Apr 06 '15 at 06:59
  • OP ask question what is the wrong in code ? but OP not ask why ? – Karthick Rajan Apr 06 '15 at 07:34
  • Yeah... **what's** wrong with the code? You didn't tell them what was wrong. You just told them to do what you said in your code. Look at the accepted answer. It clearly explains what is wrong, and also how to fix it. You just give a bunch of things to do, but don't explain **why** you're supposed to do it. – rayryeng Apr 06 '15 at 07:35
  • ok i got it next time i will give correct things with correct flow . any way very thanks for point out my mistake – Karthick Rajan Apr 06 '15 at 07:56