![]() |
如何在MATLAB GUI中获得分段线性函数
我想在MATLAB 2010a中实现一个GUI,使用户能够交互输入[URL="http://en.wikipedia.org/wiki/Piecewise_linear_function"]逐段线性函数[/URL] (通过单击添加/删除点以及通过拖放移动点)。 [URL="http://www.codeproject.com/KB/graphics/contrastStretch.aspx"]这[/URL]是C#中的实现。
我希望MATLAB中有类似的实现,它使用轴或捕获鼠标事件并更新分段函数的任何其他对象。以下是一些用户输入作为分段线性函数的示例: [IMG]https://i.stack.imgur.com/tbW3N.png[/IMG][IMG]https://i.stack.imgur.com/CqViX.png[/IMG][IMG]https://i.stack.imgur.com/LuzFG.png[/IMG][IMG]https://i.stack.imgur.com/qMIxT.png[/IMG] [B]回答:[/B] 将下面的函数保存到名为[I]addPoint.m[/I]的路径上的m文件中,然后在命令行中输入以下内容: >> hFigure =数字;>> hAxes =轴('Parent',hFigure);>> set(hAxes,'ButtonDownFcn',@addPoint);这将创建一个轴,该轴将在每次单击轴时执行[B]addPoint[/B] 。如果尚不存在线,则[B]addPoint将[/B]创建一条线,获取单击点的坐标并将这些坐标添加到该线的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); 您可以通过防止在第一次单击后自动更新轴限制来改善这一点。 [url=https://stackoverflow.com/questions/5463290]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 23:22。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.