Skip to content

Instantly share code, notes, and snippets.

@jcchurch
Last active October 12, 2015 21:18
Show Gist options
  • Save jcchurch/4088543 to your computer and use it in GitHub Desktop.
Save jcchurch/4088543 to your computer and use it in GitHub Desktop.
Quickly plot matlab data.
function quickPlot(varargin)
% Quickly plot point clouds of data.
% This works for up to 5 datasets of
% 1D or 2D or 3D data, regardless of the
% row-order or column-order format.
%
% Also works with 2D or 3D matrices.
%
% How to use this function:
% --> Give it data. That's pretty much it.
n = size(varargin, 2);
disp('Color order: r g b c m')
colors = char('r.', 'g.', 'b.', 'c.', 'm.');
num_colors = size(colors, 1);
hold on;
for i = 1:n
a = varargin{i};
color = colors(mod(i-1,num_colors)+1,:);
% 2D matrices (which includes 1D, 2D, and 3D plots)
if length(size(a)) == 2
% 1D Plots
if size(a, 1) == 1 | size(a, 2) == 1
if length(a) == 2
if size(a, 1) == 2
plot(a(1,:), a(2,:), color);
else
plot(a(:,1), a(:,2), color);
end
else
n = sum(size(a)) - 1;
plot(1:n, a, color);
end
else
%%%%% 2D Plots
if size(a, 1) == 2
plot(a(1,:), a(2,:), color);
elseif size(a, 2) == 2
plot(a(:,1), a(:,2), color);
else
%%%%%%%%% 3D Plots
if size(a, 1) == 3
plot3(a(1,:), a(2,:), a(3,:), color);
elseif size(a, 2) == 3
plot3(a(:,1), a(:,2), a(:,3), color);
else
%%%%%%%%%%%%% Grayscale
figure, imshow(a)
end
end
end
end
% 3D Plots
if length(size(a)) == 3
P = [];
for i = 1:size(a, 1)
for j = 1:size(a, 2)
for k = 1:size(a, 3)
if a(i,j,k) > 0
P = [P [i;j;k]];
end
end
end
end
if sum(size(P)) > 0
plot3(P(1,:), P(2,:), P(3,:), color);
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment