help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Assigning Graphics Handles using Variables


From: Jordi Gutiérrez Hermoso
Subject: Re: Assigning Graphics Handles using Variables
Date: Sat, 6 Aug 2011 16:34:50 -0500

On 6 August 2011 15:12, Jason C. Wells <address@hidden> wrote:
> Graphics handles are a little confusing to me. I would like to use
> an incrementing variable to setup graphic properties for a series of
> curves.
>
>     numlines=3;
>     i=1;
>     color={"green", "red", "blue"};
>     while (i <= numlines)
>        linestr = strcat ( "line", num2str ( i ), "_h" );
>        linestr = line;    # <==== this doesn't work, linestr is re-assigned.
>        set (linestr, "color", color{i});
>        set (linestr, "linewidth", 2);
>        ++i;
>     endwhile
>
>
> I would like to have linestr evaluated before setting it equal to
> the handle returned by line(). That is, I would like to set:
>
>     line1_h = line
>
> not:
>
>     linestr = line
>
> as the above code does.  line1_h is the incrementing variable.

I have often seen people attempt to do this. In effect, what you want,
is a container object, an array. You're probably translating
mathematical code where you have something similar to foo_i where i is
your index, or you're thinking like a mathematician, where you think
that the index is attached to the name instead of thinking of indexed
variables as being elements of an array.

In essence, all you have to do is instead of trying to write the index
into the variable, to use it as an index into an array. You can also
use a for loop instead of your while loop to save some typing. I would
rewrite your code like this:

    numlines=3;
    color={"green", "red", "blue"};
    for i=1:numlines
      line_h(i) = line;
      set (line_h(i), "color", color{i});
      set (line_h(i), "linewidth", 2);
    endfor

> I am not a programmer by training.

I think few of us are. It's mostly by hobby and self-teaching that
most of us pick up programming.

That being said, if I may so advertise, you sound like the target
audience for the recently published Octave For Beginners Book
(disclaimer: I was a technical reviewer this book, but I'm not getting
any sort of sponsorship or royalties for advertising it):

     http://www.packtpub.com/gnu-octave-beginners-guide/book

> Also, what does the number in a graphics handle represent? If I
> get() graphics properties, I'll see handles like -4406.21. As far as
> I can tell, that number is not meant for human consumption.

You're right, it's not. It's just some number (a handle) that
identifies a graphics object.

HTH,
- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]