user1574598
user1574598

Reputation: 3879

Problems With GUI Handles - Matlab

I am building a gui in Matlab programatically which plots data using a gui listbox in conjunction with the scatter function.

My main script that adds the axes object component to the figure is as follows:

%% create figure
fig_h = figure;
set(fig_h, 'Position', [100, 100, 1049, 895]);

%% create plot
figure
axes_h = axes('Position',[0.1 0.3 0.4 0.5], 'XLim', [-4 4], 'YLim', [-4 4]);
xlabel(axes_h, 'Valence', 'FontSize', 12, 'FontWeight', 'bold');
ylabel(axes_h, 'Arousal', 'FontSize', 12, 'FontWeight', 'bold');
grid('on');

%% create panel
panel_position = [0.55 0.3 0.32 0.5];
panel_h = uipanel('Title','Plot Music','FontSize',12,'BackgroundColor','white','Position',panel_position);

%% create listbox
list_position = [0.2 0.3 0.6 0.6];
list_h = uicontrol('Parent',panel_h,'Style','Listbox','String',tracks,'units', 'normalized','Position',list_position, 'callback', {@guiList, axes_h, valence, arousal});

%% create toggle button
toggle_position = [0 0 0.2 0.1];
toggle_h = uicontrol('Parent',panel_h,'Style', 'togglebutton', 'units', 'normalized', 'position', toggle_position, 'String', 'Hold', 'callback', @guiToggle);

The listbox_h 'callback' pertains to this function:

function guiList(list_h, evt, axes_h, valence, arousal)

val = get(list_h, 'value');
a = 40;
x = valence;
y = arousal;

for i=1:17
    if i == val
        scatter(axes_h, x(i), y(i), a,'MarkerEdgeColor',[0 .5 .5], 'MarkerFaceColor',[0 .7 .7],'LineWidth',1.5)
        axis(axes_h, [-4 4 -4 4])
        grid on
    end
end

end

I am a little confused because I am having to mirror everything that the original axes_h had setup e.g. the XLim, FontSize, etc. when the function above keeps executing. For instance, with the code as it is, the X and Y labels both disappear when guiList executes. I was hoping I could set everything up in axes_h and then use the scatter function to plot the data providing I provide it with the axes_h handle?

I am also having problems with my guiToggle function:

function guiToggle(toggle_h, evt, scatter_h)

button_state = get(toggle_h,'Value');

if button_state == 1
    hold(scatter_h, 'on');
elseif button_state == 0
    hold(scatter_h, 'off');
end

end

I'm getting this error:

Error in guiToggle (line 6)
hold(scatter_h, 'on');

Upvotes: 0

Views: 104

Answers (2)

zeeMonkeez
zeeMonkeez

Reputation: 5177

You can create an empty scatter plot when you set up your axes:

%% create plot
figure
axes_h = axes('Position',[0.1 0.3 0.4 0.5], 'XLim', [-4 4], 'YLim', [-4 4]);
scatter_h = scatter(axes_h, [],[],40, 'MarkerEdgeColor',[0 .5 .5], 'MarkerFaceColor',[0 .7 .7],'LineWidth',1.5)
xlabel(axes_h, 'Valence', 'FontSize', 12, 'FontWeight', 'bold');
ylabel(axes_h, 'Arousal', 'FontSize', 12, 'FontWeight', 'bold');
grid('on');

Then pass the handle to the scatter plot to guilist and set the XData, YData and SizeData properties:

function guiList(list_h, evt, scatter_h, valence, arousal)
    val = get(list_h, 'value');
    a = 40;
    x = valence;
    y = arousal;
    for i=1:17
        if i == val
            set(scatter_h, 'XData', x(i), 'YData', y(i), 'SizeData', a);
        end
    end
end

Upvotes: 1

MinF
MinF

Reputation: 326

Try hold on after creating the axes.

axes_h = axes('Position',[0.1 0.3 0.4 0.5], 'XLim', [-4 4], 'YLim', [-4 4]);
xlabel(axes_h, 'Valence', 'FontSize', 12, 'FontWeight', 'bold');
ylabel(axes_h, 'Arousal', 'FontSize', 12, 'FontWeight', 'bold');
grid('on');
hold on

Upvotes: 0

Related Questions