0
votes

I am using a function that I wrote that loads and analyzes time-series data, and plots a figure with subplots if the series contains something interesting.

Here's the plotting part of the code:

if ~isempty(failure_x)
    figure(call_n)
    sgt = sgtitle(filename);
    sgt.FontSize = 10;

    subplot(5,1,1)
    scatter([1: length(max_vol)], max_vol, '.')
    set(subplot(5,1,1), 'XLim', [1, length(max_vol)])

    subplot(5,1,2)
    scatter([1:length(flow)], flow, '.')
    set(subplot(5,1,2), 'XLim', [1, length(max_vol)])

    subplot(5,1,3)
    scatter([1: length(hlfv)], hlfv, '.')
    set(subplot(5,1,3), 'XLim', [1, length(max_vol)])

    subplot(5,1,4)
    scatter([1: length(stats)], stats(:,2), '.')
    set(subplot(5,1,4), 'XLim', [1, length(max_vol)])

    subplot(5,1,5)
    scatter(failure_x, failure_y)
    set(subplot(5,1,5), 'XLim', [1, length(max_vol)])
end

When I call this function by itself from the command line, everything plots properly, but when I call it from a for-loop, the last subplot always seems to be the first subplot from the figure in the next function call from the for loop. Basically, at iteration 1, subplot(5,1,5) shows the data from iteration 2 subpolot(5,1,1).

I added a parameter "call_n" to force the figures to have increasing figure numbers passed in from the for loop, but this didn't seem to solve the problem.

Any ideas? I usually code in python so I'm sure I'm doing something silly.

EDIT: Here is the loop that calls the function "detect_hlfv"

for i = 1:length(fnames)
    fname = strcat(data_dir, fnames(i).name);
    failures = detect_hlfv(fname, i);
    if length(failures) > 0
        disp("FAILURE!")
        disp(fname);
    end
end
1
Please include the for loop (a minimal version thereof) in your code, so that the code reproduces the problem - Luis Mendo
Do you still get the issue if you use f = figure(call_n), and then specify this figure object as the parent for the subplots subplot( 5, 1, 1, 'parent', f )? Similarly you can force subplots to be plot parents... sp = subplot(5,1,1); scatter( sp, x, y ); - Wolfie
Thank you, but neither of these worked :( - Zachary Swartz
Minimal,Complete,Verifiable example. (minimal -> does with 2 sublplots exhibit the same behavior? Complete -> provide us with code that we can copy-paste and reproduce your problem, time is valuable. verifiable-> obviously the code should reproduce the same problem) Btw, by trying to create such an (minimal and complete) example often results in debugging your problem yourself. Hmmm, I have to say though that I would expect @Wolfie 's suggestion to work. - tryman

1 Answers

0
votes

I am an idiot! I forgot to comment out a debug line of code that plots a variable after its instantiation. Problem solved, thanks for the help!