【问题标题】:How to get a piecewise linear function in MATLAB GUI如何在 MATLAB GUI 中获得分段线性函数
【发布时间】:2011-03-28 18:21:35
【问题描述】:

我想在 MATLAB 2010a 中实现一个 GUI,用户能够以交互方式输入 Piecewise linear function(通过单击添加/删除点并通过拖放移动点)。 Here 是 C# 中的一个实现。

我希望 MATLAB 中有一个类似的实现,它使用轴或任何其他对象来捕获鼠标事件并更新分段函数。以下是用户输入作为分段线性函数的一些示例:

【问题讨论】:

    标签: user-interface matlab user-input interactive piecewise


    【解决方案1】:

    将下面的函数保存到路径上名为 addPoint.m 的 m 文件中,然后在命令行中输入以下内容:

    >> hFigure = 图; >> hAxes = axes('父', hFigure); >> set(hAxes, 'ButtonDownFcn', @addPoint);

    这将创建一个坐标区,每次单击坐标区时都会执行 addPointaddPoint 如果不存在线,则创建一条线,获取单击点的坐标并将这些坐标添加到线的XDataYData 属性中。

    function addPoint(hObject, eventdata)
    
    % Get the clicked point.
    currentPoint = get(hObject, 'CurrentPoint');
    
    % Get the handle to the plotted line. Create a line if one doesn't exist
    % yet.
    hLine = get(hObject, 'Children');
    if isempty(hLine)
        hLine = line(0, 0, ...
            'Parent', hObject, ...
            'Marker', 's', ...
            'MarkerEdgeColor', 'r');
    end
    
    % Temporarily set the axes units to normalized.
    axesUnits = get(hObject, 'Units');
    set(hObject, 'Units', 'normalized');
    
    % Get the clicked point and add it to the plotted line.
    data(:,1) = get(hLine, 'XData');
    data(:,2) = get(hLine, 'YData');
    data(end+1,:) = [currentPoint(1,1) currentPoint(1,2)];
    data = sortrows(data, 1);
    set(hLine, 'XData', data(:,1), 'YData', data(:,2));
    
    % Reset the axes units.
    set(hObject, 'Units', axesUnits);
    

    您可以通过防止在第一次点击后自动更新坐标区限制来改善这一点。

    【讨论】:

    • 谢谢。虽然这不是一个完整的解决方案,但它对我实现我想要的 GUI 是一个很好的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    相关资源
    最近更新 更多