Search code examples
plotoctaveaxis-labels

Programmatically getting the labels in Octave


In Octave, when I plot I can get the plot object. For example

pl = get(plot(...))

I can then specify the labels such as:

xlabel("something")

I'm working on an auto-grader and need to check the contents of the xlabel from the plot object pl. I'd think I'd be able to say something like

disp(all(pl.xlabel == "something"))

But, I'm getting an error saying xlabel doesn't belong to the plot object.


Solution

  • If there is only one axis object, you could get the current axis object with gca, then get the labels and such through the axis object.

    ax = gca
    disp(get(get(ax, "XLabel") "String"))
    

    If you have multiple plots on different figures and no handle to the figure, you could do the following where you get the figure, rather than directly use gca:

    p1 = plot((1:10).^2);
    p2 = plot((1:10).^4);
    xlabel('Matt');
    fig = ancestor(p1, 'figure');
    ax = get(fig, "CurrentAxes");
    disp(get(get(ax, "XLabel") "String"))