GUI 句柄的问题 - Matlab

Problems With GUI Handles - Matlab

我正在 Matlab 中以编程方式构建一个 gui,它使用图形用户界面 listbox 结合 scatter 函数绘制数据。

我将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'callback'属于这个函数:

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

创建轴后尝试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

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