在 MATLAB 中实现可选绘图的简洁方法
Concise way to implement optional plotting in MATLAB
我喜欢在测试时绘制一些东西,但对于完整的 运行 我希望以一种简单有效的方式关闭绘制。目前我在脚本顶部有一个变量如下
plotting = true;
然后,在所有有绘图的部分中,我有一些类似于
的内容
if plotting
figure;
plot(x,y)
...
end
所以当然,如果我不想绘制,我只需设置 plotting = false;
在脚本的顶部。
有没有更好、更有效的方法?或者有没有人有他们使用的不同方法?
您可以添加以下行:
set(0,'DefaultFigureVisible','off')
在代码的开头隐藏数字。
通过设置撤消此操作:
set(0,'DefaultFigureVisible','on')
请注意,图形和绘图仍然是创建的,只是隐藏了。
示例:
set(0,'DefaultFigureVisible','off') % Visible off
figure(6); % This figure will be created but hidden
plot(6:8); % Plot is created but on the hidden figure
set(0,'DefaultFigureVisible','on') % Visible on
figure(6); % The figure will now be shown, with the previous plot.
如评论中所述,您当前的方法已经差不多了……这里有关于该方法的注释和替代方法。
保持编辑器警告整洁
如果您使用显示的语法,MATLAB 编辑器将在您的 if
语句的第一行下划线,即布尔值上没有比较运算符:
plotting = false;
if plotting
figure % <- this line will be underlined orange
% as the editor "knows" it will never be reached!
% ...
end
快速修复是使用等号比较 (==
),编辑器不会以相同的方式进行检查。这样也比较明确,稍微清楚一点,方便以后参考:
plotting = false;
if plotting == true
figure % <- this line is now not highlighted
% ...
end
使用数字数组
您在问题中使用了 "efficient" 这个词。您不会找到比上面的两行代码更有效的方法,但您可能想尝试一下图形数组。此方法允许您指定某些图形进行绘图,这意味着您可以拥有一组可选图形:
plotting = [1 3]; % We want to plot figures 1 and 3
if any(plotting == 1)
figure(1); % do stuff with figure 1
end
if any(plotting == 2)
figure(2); % won't enter this condition because plotting = [1 3]
end
if any(plotting == 3)
figure(3); % do stuff with figure 3
end
如果你不想绘制任何东西,只需设置 plotting = []
;
请注意,如果您有许多相似的图形,则可以将上述 3 个条件句放在一个简单的循环中,每个图中都有微小的变化(由进一步的 if
语句决定)。
我喜欢在测试时绘制一些东西,但对于完整的 运行 我希望以一种简单有效的方式关闭绘制。目前我在脚本顶部有一个变量如下
plotting = true;
然后,在所有有绘图的部分中,我有一些类似于
的内容if plotting
figure;
plot(x,y)
...
end
所以当然,如果我不想绘制,我只需设置 plotting = false;
在脚本的顶部。
有没有更好、更有效的方法?或者有没有人有他们使用的不同方法?
您可以添加以下行:
set(0,'DefaultFigureVisible','off')
在代码的开头隐藏数字。
通过设置撤消此操作:
set(0,'DefaultFigureVisible','on')
请注意,图形和绘图仍然是创建的,只是隐藏了。
示例:
set(0,'DefaultFigureVisible','off') % Visible off
figure(6); % This figure will be created but hidden
plot(6:8); % Plot is created but on the hidden figure
set(0,'DefaultFigureVisible','on') % Visible on
figure(6); % The figure will now be shown, with the previous plot.
如评论中所述,您当前的方法已经差不多了……这里有关于该方法的注释和替代方法。
保持编辑器警告整洁
如果您使用显示的语法,MATLAB 编辑器将在您的 if
语句的第一行下划线,即布尔值上没有比较运算符:
plotting = false;
if plotting
figure % <- this line will be underlined orange
% as the editor "knows" it will never be reached!
% ...
end
快速修复是使用等号比较 (==
),编辑器不会以相同的方式进行检查。这样也比较明确,稍微清楚一点,方便以后参考:
plotting = false;
if plotting == true
figure % <- this line is now not highlighted
% ...
end
使用数字数组
您在问题中使用了 "efficient" 这个词。您不会找到比上面的两行代码更有效的方法,但您可能想尝试一下图形数组。此方法允许您指定某些图形进行绘图,这意味着您可以拥有一组可选图形:
plotting = [1 3]; % We want to plot figures 1 and 3
if any(plotting == 1)
figure(1); % do stuff with figure 1
end
if any(plotting == 2)
figure(2); % won't enter this condition because plotting = [1 3]
end
if any(plotting == 3)
figure(3); % do stuff with figure 3
end
如果你不想绘制任何东西,只需设置 plotting = []
;
请注意,如果您有许多相似的图形,则可以将上述 3 个条件句放在一个简单的循环中,每个图中都有微小的变化(由进一步的 if
语句决定)。