【问题标题】:Using MATLAB dot notation to set multiple properties at once使用 MATLAB 点表示法一次设置多个属性
【发布时间】:2015-06-25 05:19:53
【问题描述】:

最近 MATLAB 启用了绘图句柄以使用点表示法来设置属性。

例如

set(plotLeft,'marker','o');

现在可以

plotLeft(1).Marker = 'o';

是否可以使用这种新的点表示法一次设置多个字段。下面是一些代码示例:

clc; clear all;
x = logspace(-3,0,100)';
plot1 = sin(x);
plot2 = cos(x);
[hax,plotLeft,plotRight] = plotyy(x,[plot1 plot1],x,[plot2 plot2])
plotLeft(1).Marker = 'o';
plotLeft(2).Marker = 'x';

我想设置这个位:

plotLeft(1).Marker = 'o';
plotLeft(2).Marker = 'x';

但在一行中。我可以通过以下方式访问标记类型:

plotLeft([1 2]).Marker

但它不会让我设置它们我认为它会如何工作:

>> plotLeft([1 2]).Marker = ['o' 'x']
Insufficient number of outputs from function on right hand side of equal sign to
satisfy overloaded assignment.

【问题讨论】:

    标签: matlab plot


    【解决方案1】:

    你可以使用deal函数来实现:

    [plotLeft([1 2]).Marker] = deal('o', 'x');
    

    plotLeft([1 2]).Marker 创建了一个comma-separated list 所以不能直接赋值给它,但是可以用deal来处理,相当于这样:

    [plotLeft(1).Marker, plotLeft(2).Marker] = deal('o', 'x');
    

    【讨论】:

    • 谢谢,没听说过这个交易。我只是希望有一种更快的方式,就像我写的那样,因为这种方式有点收回点符号的用处,并回到我们如何“设置”事物。
    • 由于在这种情况下plotLeft 只包含两个元素,您也可以使用更简单的[plotLeft.Marker] = deal('o','x')。不需要([1,2]) 索引。
    • 如果要多次分配相同的标记类型,另一种可能有用的语法是m = {'o', 'x'}; [plotLeft.Marker] = m{:};
    猜你喜欢
    • 2012-03-08
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2020-01-24
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    相关资源
    最近更新 更多