# MATLAB – Controlling Plots: Cells, For Loops, and Switch Statements

One thing I like to do when visualizing data is to have a bunch of different plot options. Take, for example, the movement of a soccer ball on a field. One could have time, x position, y position, z position, pitch, roll, yaw, x velocity, y velocity, etc etc. So you write your code and make figures pop up with x position vs. time, y position vs. time, z position vs. time etc etc. And when you hit the run button, your screen fills with over a dozen figures gloriously popping up like a VH1 music video from the 90s. Sometimes you don’t need all the information from the figures, but it can be a hassle to go through and comment all of them out. To mitigate this, I use the following structure:

```% This goes at the top so its easy to change
plotter = {'opt1', 'opt2', 'opt4'};

% ========  Processing  =========
% The main processing code goes here

% ======== Visualization ========

% Main plotting loop
for ii = 1:numel(plotter)
switch plotter{ii}

case 'opt1'
% Plot functions go here (i.e. plot(x, y))

case 'opt2'
% Plot functions go here (i.e. plot(x, y))

case 'opt3'
% Plot functions go here (i.e. plot(x, y))

case 'opt4'
% Plot functions go here (i.e. plot(x, y))
end
end```

What does the code do? When you hit ‘run’, the `for` loop will run through the three different plot options within the `plotter` variable. The first cell within `plotter` holds the string `'opt1'`, so it will run the plot code within the switch statement case `'opt1'`. The second cell is `'opt2'` so the plot code in the `'opt2'` switch statement case will evaluate. However, there is no `'opt3'` in `plotter`, so that plot option is skipped entirely. This gives the code writer a way to pick and choose what plots they want to have pop up on the screen. In this case, we didn’t want plot option 3 to run, so we just left out `'opt3'` from the `plotter` cell array.

#### Saving Plots

Another thing that I dislike doing is manually going through each plot, picking and choosing which ones I want to save — especially when I know beforehand which specific plots I want to save. So the plot option code can be modified to include a `save` case. My way of doing it looks like this:

```% This goes at the top so its easy to change
plotter = {'opt1', 'save', 'opt2', 'opt4'};

% ========  Processing  =========
% The main processing code goes here

% ======== Visualization ========

% Initialize figure arrays
fig_handle = zeros(size(plotter));
fig_name = cell(size(plotter));

% Main plotting loop
for ii = 1:numel(plotter)
switch plotter{ii}

case 'opt1'
fig_handle(ii) = figure;
fig_name{ii} = 'plot_option_1';
% Plot functions go here (i.e. plot(x, y))

case 'opt2'
fig_handle(ii) = figure;
fig_name{ii} = 'plot_option_2';
% Plot functions go here (i.e. plot(x, y))

case 'opt3'
fig_handle(ii) = figure;
fig_name{ii} = 'plot_option_3';
% Plot functions go here (i.e. plot(x, y))

case 'opt4'
fig_handle(ii) = figure;
fig_name{ii} = 'plot_option_4';
% Plot functions go here (i.e. plot(x, y))

case 'save'
for jj = 1:ii-1
saveas(fig_handle(jj), fig_name{jj}, 'png')
end

end
end```

So what’s new in this code? Well, as we can see, in the `save` case, we have a simple `for` loop. This loop starts at one, and goes to the previous iterator in the `plotter` loop. Because of this, we call MATLAB’s `saveas` function on all the figure handles from the start of the plotter cell array up until the `'save'` in `plotter` saving each figure by the value stored in `'fig_name'`. Thus, in the code above, the first figure will be saved with the name `'plot_option_1.png'` in the current directory. Plot option 2 and plot option 4 will still pop up as figures, but they won’t be saved.