Search code examples
matlabfunctionworkspace

Use workspace variables in a Matlab function


I'd like to use the data that are loaded to my workspace in a Matlab function. This is the beginning of my function.

function [totalProfit] = compute(p,exit)

%% Declaration of variables

entry=0;
T = length(data);
.
.
.
end

I'm getting an error:

Undefined function or variable 'data'.

Where is the error?


Solution

  • The variable data was probably defined outside of the function, so it is out of scope.

    Pass data as a parameter to compute and then it will be available inside the function.