Modify function to take input argument of marker

1 次查看(过去 30 天)
In this data cluster function the plot marker by default is '.'
I want to add a input argument to the function that will let me set the marker manually for separate inputs.
The modified function will be of the form
function []=PlotClusters(Data,IDX,Centers,point_marker,Colors)
Can someone please tell how to do this?

采纳的回答

Adam Danz
Adam Danz 2019-5-14
编辑:Adam Danz 2019-5-14
I don't recommend editing the source code because 1) you'll have to repeate those edits every time the file is updated and 2) it's a lot more work than the first proposal below.
Proposal 1: change the marker type after plotting
Instead, just identify all of the points marked by '.' and replace the marker type.
PlotClusters(Data,IDX,Centers,Colors); % run the code
axh = gca; % get the axis handle
h = findobj(axh, 'Marker', '.'); % find all objects that have marker '.'
set(h, 'Marker', 'x') % change the marker type
Proposal 2: change the function
If you must edit the source code to specify the marker type, you'll need to make these changes:
% 1) Replace the first line
% function []=PlotClusters(Data,IDX,Centers,Colors)
function []=PlotClusters(Data,IDX,Centers,Colors,Marker)
%2) Replace this line
% plot(Data(IDX == i,1),Data(IDX == i,2),'.','Color',Colors(i,:))
plot(Data(IDX == i,1),Data(IDX == i,2),Marker,'Color',Colors(i,:))
%3) Replace this line
% plot3(Data(IDX == i,1),Data(IDX == i,2),Data(IDX == i,3),'.','Color',Colors(i,:))
plot3(Data(IDX == i,1),Data(IDX == i,2),Data(IDX == i,3),Marker,'Color',Colors(i,:))
%4) Replace this line
% case 4 %All data is given just need to check consistency
case {4,5} %All data is given just need to check consistency
%5 After the switch add these lines
if nargin < 5 || isempty(Marker)
Marker = '.';
end
* Not tested!
** I recommend the first proposal!
  2 个评论
Meg Cullen
Meg Cullen 2019-5-15
I tried out both the proposals and Proposal 1 is actually much easier. Thank you very much. :)
Adam Danz
Adam Danz 2019-5-15
Good choice. The first one is definitely the way to go.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by