Daan
Daan

Reputation: 53

matrix as output of a function

Maybe a very easy question, but I am already looking for hours on the Internet for the answer but I cannot find it.

I have created the function below. In another m-file I want to use the matrix 'actual_location'. However, it is not possible to use an individual cell of the matrix (i.e. actual_location(3,45) or actual_location(1,2)). When I try to use an individual cell, I get the following error : ??? Error using ==> Actual_Location Too many input arguments.

Can anyone please tell me what I have to change, so that I can read individual cells of the matrix?

function [actual_location] = Actual_Location(~);  
actual_location=zeros(11,161);
for b=1:11  
   for t=1:161  
       actual_location(b,t) = (59/50)*(t-2-(b-1)*12)+1;   
       if actual_location(b,t) < 1  
           actual_location(b,t) =1;  
       end       
   end  
   actual_location(1,1)  
end

Upvotes: 5

Views: 10299

Answers (2)

High Performance Mark
High Performance Mark

Reputation: 78374

As you have defined it, the name in the m-file for the matrix written by your function Actual_Location is actual_location. However, when you call your function you can give the output any name you like. I presume that you are calling it like this, remembering that Matlab is a bit case-sensitive:

actual_location = Actual_Location(arguments);

You are just writing to confuse yourself. Use a name other than actual_location for the dummy argument in the function definition, and call the function to return to a variable with a more distinct name, something like this:

output = Actual_Location(arguments);

It appears that you may be expecting actual_location(1,1) to return element 1,1 of an array, whereas it is, probably, a function call with 2 input arguments.

Upvotes: 1

vicatcu
vicatcu

Reputation: 5887

This seems to suggest you are calling the Actual_Location function with to many arguments... I'm re-writing your code with proper indentation.

function [actual_location] = Actual_Location()
  actual_location=zeros(11,161); 
  for b=1:11
    for t=1:161
      actual_location(b,t) = (59/50)*(t-2-(b-1)*12)+1;
      if actual_location(b,t) < 1
        actual_location(b,t) = 1;
      end
    end
    actual_location(1,1)
  end

Upvotes: 1

Related Questions