MATLAB: Plotting several graphs in a single one

graphplotsave

*Actually I have an optimization script. After running this script I get a graph. My question is how can I plot three graphs after running the script three times. It is obvious that I can not use from the *hold on and subplot. One idea is saving the date after each run and plotting them, but I do not know how can I save the data after each step and then use them to plot a final graph.**

Best Answer

  • Put this in your script:
    if exist('figureNumber', 'var')
    % Increment figure number.
    figureNumber = figureNumber + 1;
    else
    % Doesn't exist yet. Declare figureNumber.
    % Start with figure #1.
    figureNumber = 1;
    end
    figure(figureNumber);
    BUT MAKE SURE YOU DON'T HAVE A CLEAR ALL ANYWHERE IN YOUR SCRIPT!