简体   繁体   中英

Octave error: can't read a variable in .m file

I have a 'data' file, 'lab1.m' in my working directory. Here the content of them.

data:

0 1 2 3 4

2 0 9 2 7

5 6 3 4 7

lab1.m:

load data
function y = RSSI_to_dBm(x, z)

    y = data(z, x);

end

However, there's an error when I call RSSI_to_dBm(2, 2):

octave:30> RSSI_to_dBm(2, 4)

error: `data' undefined near line 3 column 6

error: called from:

error: RSSI_to_dBm at line 3, column 4

How can I solve it? And how can I load 'data' to a variable name such as 'A'? Thank you for answering.

I'm a bit confused as well, but I would look into this:

"error: `data' undefined near line 3 column 6"

Because you only have 5 columns in your data array, but the error is referring to column 6.

Find column 6 and you may find your problem. Make sure the dimensions of the data matrix match the dimensions that the "x" and "z" parameters take. If they are askew, this can cause problems in MATLAB. I've found it easiest to monitor the dimensions of my matrices by using the debugger.

Hope that helps you in the right direction.

The problem is that data is being interpreted as a variable name, but there is no variable called data . The problem is easily solved by putting the file name in quotes:

load "data";

You might need to specify the file extension, and you can assign the result to a variable in the usual way:

A = load "data.txt";

I have tried out a solution:

load data
function z = RSSI_to_dBm(x, y, data)

    z = data(x, y);

end

It works by passing the "data" to the function every time I call it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM