Undefined function or variable

Hello everyone,
I have written following code to calculate the production costs. The price_matrix is a 360x13 double matrix and the variable a is a binary variable.
The price_matrix is stored in the base workspace. The error "Undefined function or variable 'price_matrix'." occurs. How do I import the matrix in my function.
function [total_costs] = costs_calculation (a)
t=1;
while t<=360
ct=price_matrix(t,2)*a(1,1) +price_matrix(t,3)*a(1,2) + price_matrix(t,4)*a(1,3)+price_matrix(t,5)*a(1,4) +price_matrix(t,6)*a(1,5) + price_matrix(t,7)*a(1,6) +price_matrix(t,8)*a(1,7) +price_matrix(t,9)*a(1,8) + price_matrix(t,10)*a(1,9) +price_matrix(t,11)*a(1,10) +price_matrix(t,12)*a(1,11) + price_matrix(t,13)*a(1,12);
total_costs=total_costs + ct;
end
end
Thanks a lot in advance.

3 Comments

Alexander - where are you calling the costs_calculation function from? If calling it from the base workspace (command line) then just pass in the price_matrix as an input parameter to this function. If you are not calling this function from the base workspace, then how/where is it being called from (and why is the matrix defined in the base workspace...)?
Hey Geoff, thanks for your quick response.
I'm would like to call the function from with the command line. I set the price_matrix as an input parameter.
function [total_costs] = costs_calculation (a,price_matrix)
In the command line I wrote
costs_calculation ([0 0 0 0 0 0 0 0 1 0 0], [])
The error "Index in position 1 exceeds array bounds." occurs. So the first error should be solved.
Can I set my Input-Variables this way? How do I describe the matrix in the command line, so that all values can be accessed?
Alexander, not sure you saw my Answer down in the official "Answers" section below. Did you?

Sign in to comment.

 Accepted Answer

Try this from your main script:
total_costs = costs_calculation(a, price_matrix)
The reason is that each function has it's OWN workspace. It does not automaticaly see the workspace of the base MATLAB or of other function, or of the global workspace. It only sees what's created inside of it, or passed into it. So if you want price_matrix to be seen inside of costs_calculation(), you must pass it in.

2 Comments

I put the whole matrix in the script of the funtion. My function is working but doesnt look well. So a question for the future is how do I insert extern matrices in my function. Is there a workspace only for my function?
Every script uses the shared "base" workspace. So if script1.m defines x to some value, then script2.m can access x and get the same value.
Every function will have its own private workspace and does not see variables in the "base" workspace unless you do something that no one recommends, so I won't mention it. Variables created in the function exist only while the function is running and are destroyed when the function exists, unless they're declared "persistent". No other function can see variables inside a function - they would need to be passed out as return arguments for some other code to see them.
There is also a "global" workspace that holds variables that can only be seen by functions or scripts that use the keyword "global" followed by the variables that they want to see. For example
global x
global a
"External" matrices can be stored in a variety of places:
  1. A 'mat file created with save()
  2. An Excel workbook
  3. A text file, like a standard CSV, file or your own custom format
  4. A binary file that you can read with fread()
  5. An image file that you can read with imread() or dicomread()
  6. Proprietary formats that you can read with some proprietary reader function.
There may be other ways I don't recall right now.
Does that explain things better?

Sign in to comment.

More Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!