Error using patch in MATLAB - matlab

I am creating a drag box to zoom in that uses the patch function. I get the following error when I drag:
Error using patch
Not enough input arguments.
Error in boxReady (line 31)
guiele.dragBox = patch(guiele.ResponsePlotAxis, ...
repmat(vabls.CurrentPoint(1,1),[1 4]), ...
repmat(vabls.CurrentPoint(1,2),[1 4]));
Here's the code I'm using:
% This is the point the cursor is at when the user presses down. drawBox is called again
% when the button is released and the current point then is the other corner of the patch
vabls.CurrentPoint = get(guiele.ResponsePlotAxis,'CurrentPoint');
set(guiele.ResponsePlotLine,'erasemode','none');
XYLims=[get(guiele.ResponsePlotAxis,'xlim') get(guiele.ResponsePlotAxis,'ylim')];
axes(guiele.ResponsePlotAxis);
hold on;
if ishandle(guiele.dragBox)
delete(guiele.dragBox);
end
guiele.dragBox = patch(guiele.ResponsePlotAxis, ...
repmat(vabls.CurrentPoint(1,1),[1 4]), ...
repmat(vabls.CurrentPoint(1,2),[1 4]));
set(guiele.dragBox,'FaceColor','none','EdgeColor','r','LineStyle',':');
% initialize some varaiables
guiele.ResponsePlotAxis=-1;
guiele.dragBox = -1;

The three-argument form for patch (or 4-argument form including an axes handle) requires that you also enter color data for each patch:
patch(X, Y, C);
% Or ...
patch(ax, X, Y, C);
If you don't want to enter color data, you can use the following form:
patch(ax, 'XData', X, 'YData', Y);
So your call to patch would look something like this:
guiele.dragBox = patch(guiele.ResponsePlotAxis, ...
'XData', repmat(vabls.CurrentPoint(1, 1), [1 4]), ...
'YData', repmat(vabls.CurrentPoint(1, 2), [1 4]));

Related

Multiple custom graphics object inheriting from ChartContainer

I am trying to write a custom chart (or graphical object, rather), and I would like to plot several of them in a single figure. I am following this guide, but I keep getting:
Error using matlab.graphics.chartcontainer.ChartContainer
Adding TestChart to axes is not supported. Turn hold off.
In my custom class I am combining a line and a surface, but this a minimal class works as an example:
classdef TestChart < matlab.graphics.chartcontainer.ChartContainer
properties (Access = private, Transient, NonCopyable)
% Chart axes.
HandleLine(1,1) % Handle of the line object
end
properties
Axes
XData double {mustBeReal}
YData double {mustBeReal}
ZData double {mustBeReal}
end
methods (Access = protected)
function setup(this)
this.Axes = getAxes(this);
% Create line handle
this.HandleLine = plot3(this.Axes, NaN, NaN, NaN, '-o');
end
function update(this)
this.HandleLine.XData = this.XData;
this.HandleLine.YData = this.YData;
this.HandleLine.ZData = this.ZData;
end
end
end
And then, if I try creating two of them:
a = TestChart('XData', [0 0], 'YData', [0 0], 'ZData', [1 2]);
b = TestChart('XData', [0 0], 'YData', [0 0], 'ZData', [5 8]);
I get the error saying "Adding TestChart to axes is not supported. Turn hold off.".
Is it possible to create custom graphics object that can be plotted several times in the same axex ?
Thanks in advance!

Matlab: How to save multiple variable points from mouse click (with 'ButtonDownFcn') from a plot into the workspace?

I have a figure with some vertical lines added, as such:
figure
x = rand(1,41);
y = 1:41;
H(1)= plot(x,y,'r.');
H(2)= line([x(21) x(21)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
H(3)= line([x(3) x(3)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
H(4)= line([x(15) x(15)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
I would like to be able to click on the lines with the mouse button, and store each of the clicked line index.
The following script works but I don't know how to store each index in an array. The 'IndInWorkSpace' keeps changing for each click.
set(H, 'ButtonDownFcn', {#LineSelected, H})
function [indices] = LineSelected(ObjectH, H)
set(ObjectH, 'LineWidth', 4);
set(H(H ~= ObjectH), 'LineWidth', 2);
% Get x and y data of the highlighted lines
ind = ObjectH.XData
assignin('base','IndInWorkSpace',ind);
end
Any help will be much appreciated! Thank you!
An easy fix is to first check if there was already an index found in the base workspace. If there already is a variable IndInWorkSpace, append to it, otherwise assign a new variable.
function LineSelected(ObjectH, H)
set(ObjectH, 'LineWidth', 4);
set(H(H ~= ObjectH), 'LineWidth', 2);
% Get x and y data of the highlighted lines
ind = ObjectH.XData;
if evalin('base','exist(''IndInWorkSpace'',''var'')')
indArray = evalin('base','IndInWorkSpace');
indArray(end+1) = ind;
else
indArray = ind;
end
assignin('base', 'IndInWorkSpace', indArray);
end

Animate a MATLAB figure by pressing left and right arrow keys

I am trying to change the radius of a sphere by using left and right arrow keys, plotted in a MATLAB figure. For example by pressing right arrow, the value of radius increases by one and then plot the new sphere with the updated radius. Similarly, pressing left arrow key decreases the radius by one and then plots a smaller sphere. However, I want this change to be limited between 1 and rmax.
I got some ideas of how I can approach after reading this post but still it is not what I am looking for. Therefore, I used two global variables to achieve this task in order to somehow pass the information by reference to KeyPressFcn so when a key is pressed KeyPressFcn know what those limits are. In the example below, the code does increase and decrease the radius by one but it is it does not restrict the change of radius within the specified range after left and right arrows are hit.
Is there a better way of approaching this? How can I pass the value of radius and its limits to KeyPressFcn? I want KeyPressFcn to know how much it can change the radius when left and right arrows are pressed.
function animateme()
fig_h = figure;
set(fig_h,'KeyPressFcn', #key_pressed_fcn);
global r rmax
p0 = [0 0 0];
[x,y,z] = sphere;
rmax = 10;
r = 1;
while 1==1
h = surf(x*r+p0(1), y*r+p0(2), z*r+p0(3));
set(h, 'FaceAlpha', 0.5, 'FaceColor', rand([1 3]))
axis equal;
pause
end
function key_pressed_fcn(fig_obj, eventDat)
global r rmax
if strcmpi(eventDat.Key, 'rightarrow')
r = r + 1;
if r < 1
r = 1;
end
elseif strcmpi(eventDat.Key, 'leftarrow')
r = r - 1;
if r > rmax
r = rmax;
end
end
disp(r)
First of all, don't use global variables as there is (almost) always a better way of accomplishing the same thing.
Here is an example by using nested functions which automatically have access to the variables within the parent function's workspace.
function animateme()
fig = figure();
hax = axes('Parent', fig);
set(fig, 'KeyPressFcn', #keypress)
p0 = [0,0,0];
[x,y,z] = sphere();
% Specify limits here which are accessible to nested functions
rmax = 10;
r = 1;
h = surf(x,y,z, 'Parent', hax);
% Subfunction for re-plotting the data
% This prevents you from needing a while loop
function redraw()
set(h, 'XData', x * r + p0(1), ...
'YData', y * r + p0(2), ...)
'ZData', z * r + p0(3));
set(h, 'FaceAlpha', 0.5, ...
'FaceColor', rand([1 3]))
axis(hax, 'equal')
drawnow
end
% Go ahead and do the first redraw
redraw();
% Callback to process keypress events
function keypress(~, evnt)
switch lower(evnt.Key)
case 'rightarrow'
r = min(r + 1, rmax);
case 'leftarrow'
r = max(1, r - 1);
otherwise
return
end
% Always do a redraw
redraw();
end
end
Another option is to store the current value of r within the graphics objects themselves using the UserData field. So you could put it in the surf plot itself. This is actually my preferred method because then your callback function can live anywhere and still have access to the data it needs.
function animateme()
fig = figure();
% Data to store for plotting
data.p = [0,0,0];
data.r = 1;
data.rmax = 10;
% Create a blank surface for starters
h = surf(nan(2), nan(2), nan(2));
set(h, 'UserData', data);
% Update the display of the surface
redraw(h);
% Set the callback and pass the surf handle
set(fig, 'KeyPressFcn', #(fig, evnt)keypress(h, evnt))
end
function redraw(h)
% Get the stored data from the graphics object
userdata = get(h, 'Userdata');
[x,y,z] = sphere();
set(h, 'XData', x * userdata.r + userdata.p(1), ...
'YData', y * userdata.r + userdata.p(2), ...
'ZData', z * userdata.r + userdata.p(3));
set(h, 'FaceAlpha', 0.5, ...
'FaceColor', rand([1 3]))
axis equal
drawnow;
end
function keypress(h, evnt)
% Get the stored data
userdata = get(h, 'Userdata');
switch lower(evnt.Key)
case 'rightarrow'
userdata.r = min(userdata.r + 1, userdata.rmax);
case 'leftarrow'
userdata.r = max(1, userdata.r - 1);
otherwise
return;
end
% Update the stored value
set(h, 'UserData', userdata);
redraw(h);
end

Updating plot while maintaining axes rotations?

So far I have a 3d plot that updates in realtime during a Monto-Carlo simulation of a Kessel Run. I want the plot to be rotatable while updating, but with the rotate3d flag enabled, when I call drawnow after updating the plot data, it resets the axes rotation to the default orientation, canceling whatever the user changed it to. My relevant code is as follows:
s = scatter3(pg(:,1), pg(:,2), pg(:,3), 'O', 'filled');
set(s, 'MarkerEdgeColor', 'none', 'MarkerFaceColor', 'k');
hold on
p_x = curr_path(:,1);
p_y = curr_path(:,2);
p_z = curr_path(:,3);
p = plot3(p_x, p_y, p_z);
set(p, 'LineWidth', 2, 'Color', 'k');
p_x = longest.path(:,1);
p_y = longest.path(:,2);
p_z = longest.path(:,3);
p = plot3(p_x, p_y, p_z);
set(p, 'LineWidth', 2, 'Color', 'r');
p_x = shortest.path(:,1);
p_y = shortest.path(:,2);
p_z = shortest.path(:,3);
p = plot3(p_x, p_y, p_z);
set(p, 'LineWidth', 2, 'Color', 'b');
sample_str = sprintf('%d samples', sample);
short_str = sprintf('shortest: %g parsecs', shortest.dist);
long_str = sprintf('longest: %g parsecs', longest.dist);
title(sprintf('%s, %s, %s', sample_str, short_str, long_str));
xlim([-10 10]);
ylim([-10 10]);
zlim([-10 10]);
hold off
drawnow
This is executed every time I update the data being drawn on the plot. What can I add to ensure that the axes rotation is maintained during an update?
I hope I understood you problem right. Well, here goes!
I suppose the problem is related to using plot3, which apparently resets the view settings of the figure to their defaults.
I verified this with the following program:
figure;
x = randn(10,3);
p = plot3(x(:,1), x(:,2), x(:,3));
drawnow
pause; %// do rotations in the figure GUI, press a button when done
x = randn(10,3);
p = plot3(x(:,1), x(:,2), x(:,3)); %// rotations reset
drawnow;
pause
You can rotate the figure when the pause is active, but when the pause is released and the second call to plot3 made, the rotation settings are reset.
A solution which avoids the reset is to directly update the XData, YData, and ZData in the already drawn graphics object. This can be achieved as follows:
figure;
x = randn(10,3);
p = plot3(x(:,1), x(:,2), x(:,3));
drawnow
pause; %// do rotations in the figure GUI, press a button when done
x = randn(10,3);
set(p, 'XData', x(:,1), 'YData', x(:,2), 'ZData', x(:,3)); %// no reset!
drawnow;
pause
So whatever code you have, use the handle of the graphics object to directly update the line properties to avoid the reset.
I had the same problem, in Matlab 2015b you can solve it in this simple way.
[az,el] = view;
scatter3(x,y,z);
view(az,el);
drawnow

MATLAB Simple Point Plots

In a while loop, I need to plot the positions (x,y) of two entities. That is, all I need to do is generate a plot with two points on it. I need to scale the plot to a specific maximum x and y value. An additional requirement is the fact that one of the points needs to have three concentric rings placed around it, each with a given radius. Additionally, this all is to happen in a loop, thus I'm hoping that only a single plot window opens and that I don't get a whole slew of windows opening (one for each loop iteration).
Basically here's the pseudo-code I'm trying (and failing!) to implement:
-> Open new plot window, with a given x and y axis
while (running) {
-> Clear the plot, so figure is nice and clean
-> Plot the two points
-> Plot the three circles around point A
}
I found several items in MATLAB's documentation, but no single plotting functions seems to do what I want, or there are instances where I inadvertently create multiple plots with only some of the data (i.e., one plot has the points and another has the circles).
here's a sample code you can use in your while loop
x0=1; y0=4;
x1=2; y1=3; % the x-y points
r=[1 2 3]; % 3 radii of concentrating rings
ang=0:0.01:2*pi;
xc=cos(ang)'*r;
yc=sin(ang)'*r;
plot(x0,y0,'.',x1,y1,'.'); % plot the point A
hold on
plot(x1+xc,y1+yc); % plot the 3 circles
% set the limits of the plots (though Matlab does it for you already)
xlim([min([x0 x1])-max(r) max([x0 x1])+max(r)]);
ylim([min([y0 y1])-max(r) max([y0 y1])+max(r)]);
hold off
you can make this work in a loop quite easily, read matlab's documentation on how to do that.
Try something like this:
r = [0.25 0.125 0.0625];
d = (1:360) / 180 * pi;
xy_circle = [cos(d)' sin(d)'];
xy_circle_1 = r(1) * xy_circle;
xy_circle_2 = r(2) * xy_circle;
xy_circle_3 = r(3) * xy_circle;
h_plot = plot(0, 0, '.k');
hold on
h_circle_1 = plot(xy_circle_1(:, 1), xy_circle_1(:, 2), '-b');
h_circle_2 = plot(xy_circle_2(:, 1), xy_circle_2(:, 2), '-r');
h_circle_3 = plot(xy_circle_3(:, 1), xy_circle_3(:, 2), '-g');
axis equal
for hh = 1:100
xy = rand(2, 2) / 4 + 0.375;
xlim = [0 1];
ylim = [0 1];
set(h_plot, 'XData', xy(:, 1));
set(h_plot, 'YData', xy(:, 2));
set(gca, 'XLim', xlim)
set(gca, 'YLim', ylim)
set(h_circle_1, 'XData', xy_circle_1(:, 1) + xy(1, 1));
set(h_circle_1, 'YData', xy_circle_1(:, 2) + xy(1, 2));
set(h_circle_2, 'XData', xy_circle_2(:, 1) + xy(1, 1));
set(h_circle_2, 'YData', xy_circle_2(:, 2) + xy(1, 2));
set(h_circle_3, 'XData', xy_circle_3(:, 1) + xy(1, 1));
set(h_circle_3, 'YData', xy_circle_3(:, 2) + xy(1, 2));
pause(1)
end
You can change the parameters as you wish.
You can use the following functions
figure; %creates a figure
hold on; %overlays points and circles
clf; %clear the figure
and use two types of markers (. and o) of various sizes for the points and circles
plot(x,y, 'b.', 'MarkerSize', 4);
plot(x,y, 'ro', 'MarkerSize', 10);
plot(x,y, 'go', 'MarkerSize', 14);
plot(x,y, 'bo', 'MarkerSize', 18);