1

I have a problem running my main function in MATLAB (the main program and calls other functions). Trying to run it gives me the error "??? Undefined function or variable 'max_x_address'". But, as you can see below, it has been declared as a global. This is really bothering me, so any help would be appreciated. (Do I have to modify my pathdef.m file? That is what the MATLAB troubleshooting says, but I followed some of the steps and it didn't help.)
The following is a skeleton of the program:

function Adaptive_Information
global HUBS, max_x_address, max_y_address;
.
.
max_x_address = 10;
.
.
end

2 Answers 2

4

You have to declare it global in every context that will use it. That is, add:

global max_x_address

To the beginning of each function which will use max_x_address, plus the base workspace (if needed).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, although I was able to fix it. The solution turned out to be to avoid declaring multiple variables on the same line. (Strange behavior from MATLAB)
2

Actually the line

global HUBS, max_x_address, max_y_address;

does not declare three global variables. The comma is a statement or expression delimiter in Matlab as is the EOL or the semicolon. So Matlab reads the above as

global HUBS
max_x_address
max_y_address;

So what it does is to declare HUBS as global. Then it tries to retrieve the value of max_x_address and print it. Finally it would retrieve the value of max_y_address and throw it away.

The correct syntax to declare more than one global variable is to delimit them by spaces (see also the documentation):

global HUBS max_x_address max_y_address

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.