Search code examples
matlabplotoctavematlab-figureoctave-gui

Octave - plotting multiple series to specific figure/axes in a loop


I am trying to plot data from multiple sensors on one figure using a for loop. Currently the code loops over multiple data files and plots the spectrogram for each of them, each in a separate figure, but I would also like to plot the PSDs of all the data together on one graph at the end. Is there a more graceful way to do this than duplicating the entire loop? In other words, can I predefine my axes somehow, e.g.

figure,
psd_plots = axes();

then as I go through my loop, plot to THAT figure specifically. Something like:

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

This seems like it should be possible based on the 'axes' object existing, but from the docs, I can't see how to actually plot to the axes once you define them, or how to associate them with a figure. Thoughts?


Solution

  • You can specify in which figure you need to plot by using figure(unique_id_of_the_figure), here is a minimal example:

    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,'visible','off')
       plot(rand(1,10))
    end
    % Display figure #1
    figure(1)