Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我想在MATLAB 2010a中实现一个GUI,使用户能够交互输入逐段线性函数 (通过单击添加/删除点以及通过拖放移动点)。 这是C#中的实现。
我希望MATLAB中有类似的实现,它使用轴或捕获鼠标事件并更新分段函数的任何其他对象。以下是一些用户输入作为分段线性函数的示例: ![]() ![]() ![]() ![]() 回答: 将下面的函数保存到名为addPoint.m的路径上的m文件中,然后在命令行中输入以下内容: >> hFigure =数字;>> hAxes =轴('Parent',hFigure);>> set(hAxes,'ButtonDownFcn',@addPoint);这将创建一个轴,该轴将在每次单击轴时执行addPoint 。如果尚不存在线,则addPoint将创建一条线,获取单击点的坐标并将这些坐标添加到该线的XData和YData属性中。 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); 您可以通过防止在第一次单击后自动更新轴限制来改善这一点。 更多&回答... |
![]() |
![]() |