【问题标题】:Etch a Sketch-like drawing in Matlab在 Matlab 中蚀刻类似草图的图形
【发布时间】:2015-07-15 08:07:16
【问题描述】:

我正在 Matlab 中制作图形用户界面并进行以下设置:

然后我在滑块回调中得到了这段代码:

plot(get(handles.slider2,'Value'),get(handles.slider1,'Value'));
plot(get(handles.slider2,'Value'),get(handles.slider1,'Value'));

因此,每次我调整左侧的滑块时,点都会垂直移动 +1。当我调整底部滑块时,点将水平移动 +1。

我真正想要的是每次调整一个滑块时画一条线。并且之前的操作仍然会在图表上可见。这样之后,您就可以看到从头到尾的整个路线。

【问题讨论】:

标签: matlab user-interface drawing axis


【解决方案1】:

这里有一些东西可以让你继续前进。代码相当重复,诀窍是:

1) 创建一个 Nx2 数组来存储移动点/线的坐标,随着滑块的移动而更新。第一列是 x 坐标,第二列是 y 坐标。

2) 创建与每个滑块关联的侦听器对象,以生成平滑、连续的绘图。

3) 发出hold on 命令后,只显示包含位置的数组的最后一行。

这里我使用散点图,但我会让你弄清楚如何使用 LineSeries 对象。这很容易:)

您还可以自定义点的外观。这里我将起点设为一个大黑点,每次释放滑块(执行回调时)都会显示一个蓝点。

function DrawMarkerLine(~)
clc
clear

hFig = figure('Position',[100 100 400 400],'Units','normalized');

%// create axes with handle
handles.axes1 = axes('Position', [0.2 0.2 0.6 0.6],'XLimMode','manual','YLimMode','manual','XLim',[-4 4],'YLim',[-4 4]);

%// create x slider with handle
handles.x_slider = uicontrol('style', 'Slider','Min',-4,'Max',4,'Value', 0,'units','normalized','position', [0.2 0.08 0.6 0.08], 'callback', @(s,e) UpdateX);
handles.SliderxListener = addlistener(handles.x_slider,'Value','PostSet',@(s,e) XListenerCallBack);


%// create y slider with handle
handles.y_slider = uicontrol('style', 'Slider', 'Min', -4, 'Max', 4, 'Value', 0, 'units', 'normalized', 'position', [0.08 0.2 0.08 0.6], 'callback', @(s,e) UpdateY);
handles.SlideryListener = addlistener(handles.y_slider,'Value','PostSet',@(s,e) YListenerCallBack);

%// Initialize Nx2 array  (x and y coordinates) containing all the positions
handles.AllPositions = [0 0];
handles.Sc =  scatter(handles.axes1,handles.AllPositions(1,1),handles.AllPositions(1,2),200,'k','filled');


%// set axis equal to the sliders min and max
set(handles.axes1, 'YLim', [-4 4], 'XLim', [-4 4],'XTick',-4:1:4,'YTick',-4:1:4);

guidata(hFig,handles);

%// Listeners callbacks followed by sliders callbacks. They are all the ame
%// basically.

    function XListenerCallBack

        handles = guidata(hFig); %// Get handles.

        %// Get position of both sliders
        xval = (get(handles.x_slider,'value'));
        yval = (get(handles.y_slider,'value'));

        %// Concatenate all values
        handles.AllPositions = [handles.AllPositions; xval yval];

        hold on

        %// Draw markers        
        scatter(handles.AllPositions(end,1),handles.AllPositions(end,2),40,'r')
        drawnow

        set(handles.axes1, 'YLim', [-4 4], 'XLim', [-4 4]); %// Set axis limits

        guidata(hFig,handles);
    end

    function YListenerCallBack

        handles = guidata(hFig);

        xval = (get(handles.x_slider,'value'));
        yval = (get(handles.y_slider,'value'));

        handles.AllPositions = [handles.AllPositions; xval yval];

        hold on
         %// Draw markers        
        scatter(handles.AllPositions(end,1),handles.AllPositions(end,2),40,'r')
        drawnow

        set(handles.axes1, 'YLim', [-4 4], 'XLim', [-4 4]); %// Set axis limits

        guidata(hFig,handles);

    end

    function UpdateY(~)

         handles = guidata(hFig); %// Get handles.

        %// Get position of both sliders
        xval = (get(handles.x_slider,'value'));
        yval = (get(handles.y_slider,'value'));

        handles.AllPositions = [handles.AllPositions; xval yval];

        hold on
         %// Draw markers        
        scatter(handles.AllPositions(end,1),handles.AllPositions(end,2),100,'b','filled')
        drawnow

        set(handles.axes1, 'YLim', [-4 4], 'XLim', [-4 4]); %// Set axis limits


        guidata(hFig,handles);

    end

    function UpdateX(~)

         handles = guidata(hFig); %// Get handles.

        %// Get position of both sliders
        xval = (get(handles.x_slider,'value'));
        yval = (get(handles.y_slider,'value'));

        handles.AllPositions = [handles.AllPositions; xval yval];

        hold on
         %// Draw markers        
        scatter(handles.AllPositions(end,1),handles.AllPositions(end,2),100,'b','filled')
        drawnow

        set(handles.axes1, 'YLim', [-4 4], 'XLim', [-4 4]); %// Set axis limits


        guidata(hFig,handles);

    end
end

样本输出:

请注意,您移动得越慢,图上的点彼此之间的距离就越近。我移动得很快,因此一些点之间的空间很大。

玩得开心!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多