Modify function to take input argument of marker

1 回表示 (過去 30 日間)
Meg Cullen
Meg Cullen 2019 年 5 月 14 日
コメント済み: Adam Danz 2019 年 5 月 15 日
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 Center および File ExchangeFormatting and Annotation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by