Why is my line object being registered as a double in app-designer Matlab?

Viewed 161

In Matlab, if I were to do this:

lines
lines(1)=plot(1:5,1:5)
lines(2)=plot(1:10,1:10)

Lines would be registered as a vector that stores line objects, however in App-Designer, I did the same thing, but it somehow interprets the objects as doubles instead of lines. In my properties I declared the global variable lines like so:

lines

I then used the property in a separate function the same as above

app.lines(1)=plot(1:5,1:5)
app.lines(2)=plot(1:10,1:10)

The problem I am having is that it is registering it as a double instead of a line object. Does anyone know why this is? If so, is there any possible fixes?

2 Answers

In the past, all graphics handles were numbers. It is only since release R2014b that these are special objects. When they introduced these objects, they made sure old code could continue working. One of the consequences is that handle graphics objects readily convert to numbers when assigning them into a numeric array. Thus,

lines = 0;
lines(1) = plot(1:5,1:5);

will convert the graphics object handle to its numeric equivalent.

This number can still be used to access the graphics object. You just need to use the old (pre-R2014b) syntax:

set(lines(1),'property',value)
get(lines(1),'property')

To make sure this conversion does not happen, initialize lines to an empty graphics object array:

lines = gobjects(0); % empty graphics array object

see the documentation for gobjects for more ways to use that function.


Once the conversion to a number has happened, you can obtain the graphics handle object again using the function handle:

lines = handle(lines);

I think I found a solution:

Declare lines as a property of app as graphics gobjects:

properties (Access = private)
    %Declare lines as array of objects of class matlab.graphics.chart.primitive.Line
    %lines matlab.graphics.chart.primitive.Line
    lines = gobjects(0); %Cris Luengo suggestion is better...  
end

You can add a property by pressing "+P" Property button.

Relevant part of App designer tutorial code:

classdef tutorialApp < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure              matlab.ui.Figure
        UIAxes                matlab.ui.control.UIAxes
        AmplitudeSliderLabel  matlab.ui.control.Label
        AmplitudeSlider       matlab.ui.control.Slider
    end


    properties (Access = private)
        % lines matlab.graphics.chart.primitive.Line
        lines = gobjects(0); %Cris Luengo suggestion is better...  
    end


    methods (Access = private)

        % Value changed function: AmplitudeSlider
        function AmplitudeSliderValueChanged(app, event)
            value = app.AmplitudeSlider.Value;
            %plot(app.UIAxes, value*peaks)
            app.lines(1) = plot(app.UIAxes, 1:5,1:5);
            app.lines(2) = plot(app.UIAxes, 1:10,1:10);
        end
    end

Display app.lines(1) in command prompt (I placed a break point in line app.lines(2)...):

K>> app.lines(1)

ans = 

  Line with properties:

          Color: [0 0.4470 0.7410]
      LineStyle: '-'
      LineWidth: 0.5000
         Marker: 'none'
     MarkerSize: 6
MarkerFaceColor: 'none'
          XData: [1 2 3 4 5]
          YData: [1 2 3 4 5]
          ZData: [1×0 double]
Related