问题描述
我正在尝试使用 for 循环在一个图形上绘制来自多个传感器的数据。目前,代码循环遍历多个数据文件并为每个文件绘制频谱图,每个文件都在一个单独的图形中,但我还想在最后将所有数据的 PSD 绘制在一个图形上。有没有比复制整个循环更优雅的方法来做到这一点?换句话说,我可以以某种方式预定义我的轴,例如
figure,psd_plots = axes();
然后当我完成我的循环时,专门绘制到那个数字。类似的东西:
for i=1:length(files):
file = fopen(files{i},'r');
data = fread(file);
# plot spectrogram in its own figure
figure,specgram(data),# add PSD to group figure
[psd,f] = periodogam(data)
plot(f,psd,axes=psd_plots)
end
这似乎基于现有的“轴”对象应该是可能的,但是从文档中,我看不到定义轴后如何实际绘制轴,或者如何将它们与图形相关联。想法?
解决方法
您可以使用 figure(unique_id_of_the_figure)
指定需要绘制的图形,这是一个最小示例:
close all
% Create the figure #1 but we do not display it now.
figure(1,'visible','off')
% Set hold to on
hold on
for ii = 1:4
% Create a new figure to plot new stuff
% if we do not specify the figure id,octave will take the next available id
figure()
plot(rand(1,10))
% Plot on the figure #1
figure(1,'off')
plot(rand(1,10))
end
% Display figure #1
figure(1)