I want to implement a GUI in MATLAB 2010a that user be able to input a Piecewise linear function interactively (add/remove points by click and move point by drag and drop). Here is an implementa开发者_开发知识库tion in C#.
I am hope there is an similar implementation in MATLAB that uses axes or any other object that capture mouse events and update the piecewise function. Below is some examples of user input as a piecewise linear function:
Save the function below to an m-file on your path called addPoint.m and enter the following at the command line:
>> hFigure = figure; >> hAxes = axes('Parent', hFigure); >> set(hAxes, 'ButtonDownFcn', @addPoint);
This creates an axes that will execute addPoint every time the axes is clicked. addPoint creates a line if no line exists already, gets the coordinates of the clicked point and adds these coordinates to the XData
and YData
properties of the line.
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);
You could improve this by preventing the axes limits from being automatically updated after the first click.
精彩评论