本周文件交换选择

我们最好的用户反馈

蜘蛛绘图和更多的参数验证

汪东城本周的选择是spider_plot通过摩西

有不少“蜘蛛情节”但摩西的这幅画引起了我的注意,原因有几个。

  • Moses在函数中包含了详细的帮助。
帮助spider_plot
用单独的轴创建一个蜘蛛或雷达图。语法:spider_plot(P) spider_plot(___, Name, Value) Input Arguments:(Required) P -用于绘制蜘蛛图的数据点。行是数据组,列是数据点。如果没有指定轴标签和轴限制,则自动生成。[vector | matrix]名称-值对参数:(可选)AxesLabels -用于指定每个轴的标签。[自动生成(默认)| cell of strings | 'none'] AxesInterval -用于改变web之间显示的间隔数。[3(默认)| integer] AxesPrecision -用于更改轴上显示值的精度级别。输入“none”删除轴文本。[1(默认)| integer | 'none'] AxesLimits -用于手动设置坐标轴限制。2 ×大小(P, 2)的矩阵。上面一行是最小轴限制,下面一行是最大轴限制。 [auto-scaled (default) | matrix] FillOption - Used to toggle color fill option. ['off' (default) | 'on'] FillTransparency - Used to set color fill transparency. [0.1 (default) | scalar in range (0, 1)] Color - Used to specify the line color, specified as an RGB triplet. The intensities must be in the range (0, 1). [MATLAB colors (default) | RGB triplet] LineStyle - Used to change the line style of the plots. ['-' (default) | '--' | ':' | '-.' | 'none'] LineWidth - Used to change the line width, where 1 point is 1/72 of an inch. [0.5 (default) | positive value] Marker - Used to change the marker symbol of the plots. ['o' (default) | 'none' | '*' | 's' | 'd' | ...] MarkerSize - Used to change the marker size, where 1 point is 1/72 of an inch. [8 (default) | positive value] FontSize - Used to change the font size of the labels and values displayed on the axes. [10 (default) | scalar value greater than zero] Examples: % Example 1: Minimal number of arguments. All non-specified, optional arguments are set to their default values. Axes labels and limits are automatically generated and set. D1 = [5 3 9 1 2]; % Initialize data points D2 = [5 8 7 2 9]; D3 = [8 2 1 4 6]; P = [D1; D2; D3]; spider_plot(P); legend('D1', 'D2', 'D3', 'Location', 'southoutside'); % Example 2: Manually setting the axes limits. All non-specified, optional arguments are set to their default values. axes_limits = [1, 2, 1, 1, 1; 10, 8, 9, 5, 10]; % Axes limits [min axes limits; max axes limits] spider_plot(P,... 'AxesLimits', axes_limits); % Example 3: Set fill option on. The fill transparency can be adjusted. axes_labels = {'S1', 'S2', 'S3', 'S4', 'S5'}; % Axes properties axes_interval = 2; fill_option = 'on'; fill_transparency = 0.1; spider_plot(P,... 'AxesLabels', axes_labels,... 'AxesInterval', axes_interval,... 'FillOption', fill_option,... 'FillTransparency', fill_transparency); % Example 4: Maximum number of arguments. axes_labels = {'S1', 'S2', 'S3', 'S4', 'S5'}; % Axes properties axes_interval = 4; axes_precision = 'none'; axes_limits = [1, 2, 1, 1, 1; 10, 8, 9, 5, 10]; fill_option = 'on'; fill_transparency = 0.2; colors = [1, 0, 0; 0, 1, 0; 0, 0, 1]; line_style = '--'; line_width = 3; marker_type = 'd'; marker_size = 10; font_size = 12; spider_plot(P,... 'AxesLabels', axes_labels,... 'AxesInterval', axes_interval,... 'AxesPrecision', axes_precision,... 'AxesLimits', axes_limits,... 'FillOption', fill_option,... 'FillTransparency', fill_transparency,... 'Color', colors,... 'LineStyle', line_style,... 'LineWidth', line_width,... 'Marker', marker_type,... 'MarkerSize', marker_size,... 'FontSize', font_size); Author: Moses Yoo, (jyoo at hatci dot com) 2019-10-16: Minor revision to add name-value pairs for customizing color, marker, and line settings. 2019-10-08: Another major revision to convert to name-value pairs and add color fill option. 2019-09-17: Major revision to improve speed, clarity, and functionality Special Thanks: Special thanks to Gabriela Andrade & Andrテゥs Garcia for their feature recommendations and suggested bug fixes.
  • 他还提供了一个演示各种用例的Live Script示例

  • 他在与用户的交流中非常活跃评论部分,并且在更新条目方面也非常勤奋。

我很高兴看到一段具有健壮的错误检查功能的代码。他有大约100行代码用于检查函数的输入错误。他还为可选参数设置默认值。

下面是代码的输入参数处理/错误检查部分。

数据属性%%%%点属性[num_data_groups, num_data_points] = size(P);%可选参数的个数Numvarargs =长度(varargin);检查名称-值对参数个数是否为偶数如果Mod (numvarargs, 2) == 1错误(错误:请检查名称-值对参数);结束创建默认标签Axes_labels = cell(1, num_data_points);遍历数据点数Ii = 1:num_data_points%默认轴标签Axes_labels {ii} = sprintf(“标签%我”(二);结束%默认参数Axes_interval = 3;Axes_precision = 1;Axes_limits = [];fill_option =“关闭”;Fill_transparency = 0.2;颜色= [0,0.4470,0.7410;...0.8500, 0.3250, 0.0980;...0.9290, 0.6940, 0.1250;...0.4940, 0.1840, 0.5560;...0.4660, 0.6740, 0.1880;...0.3010, 0.7450, 0.9330;...0.6350, 0.0780, 0.1840];line_style =“- - -”;Line_width = 2;marker_type =“o”;Marker_size = 8;Font_size = 10;检查是否指定了可选参数如果Numvarargs > 1初始化名称-值参数Name_arguments = varargin(1:2:end);Value_arguments = varargin(2:2:end);遍历名称-值参数Ii = 1:长度(name_arguments)根据名称设置值参数开关低(name_arguments {2})情况下“axeslabels”Axes_labels = value_arguments{ii};情况下“axesinterval”Axes_interval = value_arguments{ii};情况下“axesprecision”Axes_precision = value_arguments{ii};情况下“axeslimits”Axes_limits = value_arguments{ii};情况下“filloption”Fill_option = value_arguments{ii};情况下“filltransparency”Fill_transparency = value_arguments{ii};情况下“颜色”颜色= value_arguments{ii};情况下“线型”Line_style = value_arguments{ii};情况下“线宽”Line_width = value_arguments{ii};情况下“标记”Marker_type = value_arguments{ii};情况下“markersize”Marker_size = value_arguments{ii};情况下“字形大小”Font_size = value_arguments{ii};否则错误(错误:请输入有效的名称-值对。);结束结束结束%%%错误检查%%%检查axes labels是否为单元格如果iscell (axes_labels)检查轴标签是否与点的数量相同如果长度(axes_labels) ~= num_data_points错误(“错误:请确保标签的数量与点数相同。”);结束其他的检查是否有效的字符串条目如果~ strcmp (axes_labels,“没有”)错误(“错误:请输入有效标签或“none”以删除标签。”);结束结束检查轴限制是否为空如果~ isempty (axes_limits)检查轴限制的长度是否与点的数量相同。如果Size (axes_limits, 1) ~= 2 || Size (axes_limits, 2) ~= num_data_points error(“错误:请确保最小和最大轴限制与数据点数量匹配。”);结束结束检查轴精度是否为字符串如果~ ischar (axes_precision)检查轴属性是否为整数如果Floor (axes_interval) ~= axes_interval || Floor (axes_precision) ~= axes_precision error('错误:请为轴属性输入一个整数。');结束检查轴属性是否为正如果Axes_interval < 1 || axes_precision < 1 error(错误:请输入轴属性的值大于1。);结束其他的检查轴精度是否是有效的字符串条目如果~ strcmp (axes_precision,“没有”)错误('错误:无效的轴精度输入。请输入“none”删除轴文本。”);结束结束检查是否不是有效的填充选项参数如果~ ismember (fill_option, {“关闭”“上”})错误(“错误:请输入“关闭”或“打开”填充选项。”);结束检查填充透明度是否有效如果Fill_transparency < 0 || Fill_transparency > 1错误('错误:请输入一个介于[0,1]之间的透明度值。');结束检查字体大小是否大于0如果Font_size <= 0 error(错误:请输入大于零的字体大小。);结束

这可以转换成参数块。

参数P(::)翻倍选项。AxesLabels{validateAxesLabels (options.AxesLabels, P)}= cellstr (“标签”+(1:大小(P,2)))选项。AxesInterval(1,1) double {mustBeInteger}= 3个选项。AxesPrecision{validateAxesPrecision (options.AxesPrecision)}= 1选项。AxesLimits双{validateAxesLimits (options.AxesLimits, P)}=[]选项。FillOptionchar {mustBeMember (options.FillOption{‘了’,‘’})}“关闭”选项。FillTransparency(1,1) double {mustBeGreaterThanOrEqual(options.FillTransparency,0),mustBeLessThanOrEqual(options.FillTransparency,1)}= 0.1 options。颜色(:,3) double {mustBeGreaterThanOrEqual(options.Color,0),mustBeLessThanOrEqual(options.Color,1)}=得到(大的,“defaultAxesColorOrder”)选项。线型char {mustBeMember (options.LineStyle ,{'-','--',':','-.',' 没有'})}“- - -”选项。线宽(1,1) double {mustBePositive}= 2个选项。标记char {mustBeMember (options.Marker {' + ', ' o ','*','.',' x”、“广场”、“s”、“钻石”、“d”、“v ','^','>','<',' 五角星形”、“p”、“卦”、“h”、“没有”})}“o”选项。标记Size(1,1) double {mustBePositive}= 8个选项。字形大小(1,1) double {mustBePositive}= 10结束

我还为一些参数创建了自定义验证函数。

函数validateAxesPrecision (x)如果isnumeric (x) validateattributes (x) {“双”},{“标量”“整数”mfilename},“AxesPrecision”其他的如果~ isequal (x,“没有”)错误('AxesPrecision必须是标量整数或'none''结束结束结束函数validateAxesLimits (axLim P)如果~ isempty (axLim) validateattributes (axLim, {“双”},{“大小”,大小(P, 2) [2]}, mfilename,“AxesLimits”结束结束函数validateAxesLabels (axLabels P)如果~ isequal (axLabels“没有”) validateattributes (axLabels, {“细胞”},{“大小”,大小(P, 2) [1]}, mfilename,“AxesLabels”结束结束

这些更改将代码中的行数从500减少到400。很整洁!

评论

试一试,让我们知道你的想法在这里或者留下评论摩西。




由MATLAB®R2019b发布

|
  • 打印
  • 发送电子邮件

评论

要发表评论,请点击此处登录到您的MathWorks帐户或创建一个新帐户。