GUI句柄的问题 - Matlab

时间:2015-10-08 19:34:14

标签: matlab matlab-guide

我正在以编程方式在Matlab中构建gui,使用gui listboxscatter函数绘制数据。

axes对象组件添加到figure的主脚本如下:

%% 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);

listbox_h'回调'属于这个功能:

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

我有点困惑,因为我必须镜像原始axes_h设置的所有内容,例如当上述函数继续执行时,XLimFontSize等。例如,使用代码,当guiList执行时,X和Y标签都会消失。我希望我可以在axes_h中设置所有内容,然后使用scatter函数绘制数据,为我提供axes_h句柄?

我的guiToggle功能也遇到了问题:

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

我收到此错误:

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

2 个答案:

答案 0 :(得分:1)

您可以在设置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');

然后将把手传递给散点图到guilist并设置XDataYDataSizeData属性:

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

答案 1 :(得分:0)

创建轴后尝试hold on

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