cutting selection form the plot with the markers

3 views (last 30 days)
Olga
Olga on 11 Mar 2012
Answered: attao on 30 Sep 2015
Can somebody help with adding markers to the plot? I have an ECG plot loaded to the GUI and shown on the plot, it's moved with a slider to see along the whole signal. I'd like to add two markers which would be set manually by the user on the ECG plot in order to cut the selected part, which would be shown in a new figure window. Is it possible at all? Can someone tell me how sould I do this cutting and so one?
fread(FID, 4, 'uint8');
Data = fread(FID, [1, 4092], 'uint8');
Calib = Data(4:4:end);
fread(FID, 4, 'uint8');
Data = fread(FID, [4092, 204600], '4092*uint8', 4);
x1 = Data(1:2:204600);
z1 = Data(1:2:1024);
fs = 128;
N = length (x1);
M = length (z1);
t = 0:N-1/fs;
t2 = 0:M-1/fs;
axes(handles.axes1)
plot(t/fs,x1)
xlabel('Czas [s]');
ylabel('Napięcie [mV]');
axis tight
grid on
set(handles.nazwa_ekg,'string',nazwa);

Answers (2)

Image Analyst
Image Analyst on 11 Mar 2012
I've done this sort of thing before. I used two sliders to control the section of the plot I want extract. There were two lines, one on each end of the section. The sliders each used line() to draw a vertical line at the location of their end of the section. So set up two sliders, one called sldLeftEdge, and one called sldRightEdge. In the callback for those, you move the lines indicating the starting and ending location. You can also cut the data right then, or you can have a separate button that plots the data between the left and right edge. If you need example code, see below.
% --- Executes on slider movement.
function sldLeftEdge_Callback(hObject, eventdata, handles)
% hObject handle to sldLeftEdge (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
sliderValue = get(handles.sldLeftEdge, 'Value');
toolTipText = sprintf('Slider value = %.1f', sliderValue);
set(handles.sldLeftEdge, 'TooltipString', toolTipText);
set(handles.txtLeftEdge, 'String', toolTipText);
handles = SliderCallback(handles);
guidata(hObject, handles);
return;
%=====================================================================
function handles = SliderCallback(handles)
% Disable all controls. Re-enable later with GroupSet
WasEnabled = DisableAllControls_Pointer(handles, 'watch');
% Get the slider value.
sliderLeftEdgeValue = int32(round(get(handles.sldLeftEdge, 'Value')));
sliderRightEdgeValue = int32(round(get(handles.sldRightEdge, 'Value')));
% Update the label above the sliders.
newLabelLeft = sprintf('Left Edge is at %d', sliderLeftEdgeValue);
set(handles.txtLeftEdge, 'string', newLabelLeft);
newLabelRight = sprintf('Right Edge is at %d', sliderRightEdgeValue);
set(handles.txtRightEdge, 'string', newLabelRight);
% Move the vertical lines to show the new positions.
PlaceBothEdgesOnPlot(handles, sliderLeftEdgeValue, sliderRightEdgeValue);
MakeMeasurements(sliderLeftEdgeValue, sliderRightEdgeValue);
% Display the results in the info box.
% handles = DisplayResults(handles, resultsArray);
% Re-enable all those controls we disabled with DisableAllControls_Pointer
GroupSet(handles, WasEnabled, 'Enable', 'on');
set(handles.figMainWindow, 'Pointer', 'arrow');
return;
%=====================================================================
% Shows vertical lines going up from the X axis to the curve on the plot.
function PlaceBothEdgesOnPlot(handles, x1, x2)
% If the plot is visible, plot the line.
persistent handlesToVerticalBars;
if get(handles.axesPlot, 'visible')
axes(handles.axesPlot); % makes existing axes handles.axesPlot the current axes.
maxYValue = max(ylim);
% Make sure x location is in the valid range along the horizontal X axis.
XRange = get(handles.axesPlot, 'XLim');
maxXValue = XRange(2);
if x1 > maxXValue
x1 = maxXValue;
end
if x2 > maxXValue
x2 = maxXValue;
end
% Erase the old lines.
if ~isempty(handlesToVerticalBars)
try
delete(handlesToVerticalBars);
catch ME
end
end
% Draw vertical lines at the slope start and end locations.
hold on;
handlesToVerticalBars(1) = PlaceVerticalBarOnPlot(handles, x1, 'g');
handlesToVerticalBars(2) = PlaceVerticalBarOnPlot(handles, x2, 'r');
% Draw lines from curve at left edge to curve at right edge.
% handlesToVerticalBars(3) = line([x1 x2], [profileArray(x1) profileArray(x2)], 'Color', 'b');
hold off;
end
return; % End of PlaceBothEdgesOnPlot
%=====================================================================
% Shows vertical lines going up from the X axis on the plot.
function lineHandle = PlaceVerticalBarOnPlot(handles, x, lineColor)
% If the plot is visible, plot the line.
if get(handles.axesPlot, 'visible')
axes(handles.axesPlot); % makes existing axes handles.axesPlot the current axes.
% Make sure x location is in the valid range along the horizontal X axis.
XRange = get(handles.axesPlot, 'XLim');
maxXValue = XRange(2);
if x > maxXValue
x = maxXValue;
end
% Erase the old line.
%hOldBar=findobj('type', 'hggroup');
%delete(hOldBar);
% Draw a vertical line at the X location.
hold on;
yLimits = ylim;
lineHandle = line([x x], [yLimits(1) yLimits(2)], 'Color', lineColor);
hold off;
end
return; % End of PlaceVerticalBarOnPlot
  2 Comments
MIST Sarker
MIST Sarker on 24 Jul 2015
Could you please make your code executable? I can't run this.
Image Analyst
Image Analyst on 24 Jul 2015
No, sorry. It's just some callbacks from a much larger GUI application that I can't post. You'll have to build your own GUI with axes and scrollbars and put this code into your callbacks.

Sign in to comment.


attao
attao on 30 Sep 2015
is it possible to post a picture of what it would look like?

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!