简体   繁体   中英

Octave: Subscripts for the text function?

I'm running Octave 5.1.0. I try to reproduce the code on this page: https://octave.sourceforge.io/octave/function/text.html

For example, I try to reproduce Demonstration 3 there, ie this code:

 clf;
 axis ([0 8 0 8]);
 title (["1st title";"2nd title"]);
 xlabel (["1st xlabel";"2nd xlabel"]);
 ylabel (["1st ylabel";"2nd ylabel"]);
 text (4, 4, {"Hello", "World"}, ...
       "horizontalalignment", "center", ...
       "verticalalignment", "middle");
 grid on;

I get the following error message:

text( , ,, , , , ): subscripts must be either integers 1 to (2^63)-1 or logicals (note: variable 'text' shadows function)

I tried changing the code to:

clf;
 axis ([0 8 0 8]);
 title (['1st title';'2nd title']);
 xlabel (['1st xlabel';'2nd xlabel']);
 ylabel (['1st ylabel';'2nd ylabel']);
 text (4, 4, 'Hello','horizontalalignment', 'center','verticalalignment', 'middle');
 grid on;

Then I get the following error message:

text(4...[x6]...): but text has size 1x39 (note: variable 'text' shadows function)

I'm a bit stumped as to how to solve this. I also fail to reproduce other code with the text function. I started running Octave a few weeks ago, so it wouldn't be impossible that my installation failed somehow. Other functions work as expected though.

I have installed the io and statistics packages. Can they interfer somehow?

Can anybody figure out what's going on?

The key is in the error message:

note: variable 'text' shadows function

It means you have defined "text" as a variable in your workspace, and have assigned a value to it.

Therefore, now each time you try to access the text function, you are accessing your variable instead.

Clear your workspace (or just the text variable) and try again.

PS. While this is not conventional practice, I personally tend to name all my non-function variables starting with a capital letter, to avoid name-clashes with functions, since most functions in octave start with a small letter.

In general, always check that a name is not taken before you assign something to that name, to avoid 'shadowing' as in this case.

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