when will i have my story drawn in matlab?

There are a few things you may want to note. First the closing-commands.

  • clf close (current) figure
  • clear clear all variables in the current workspace (this can the the base workspace in scripts or the workspace of the function, in which the command is called)
  • close all closes all open figure windows
  • clc clear command window.

Although these are handy commands, the are meant to be used when working in the command window and one should think twice when using them in scripts and never use them in functions. Why so? You might forget that you have used them and consequently, you code behaves unexpected. In particular if you call multiple functions/scripts, which all manipulate the same windows or output.

The command figure opens a new figure window. So every time this command is issued, a new window pops up and is set to “active”, i.e. it is automatically the current figure. It returns its specific figure handle if you assign an output to it: fh = figure('Name','Figure A'). If you later what to activate a specific figure, you can call its handle: figure(fh). The function gcf always returns the handle of the current figure.

Now, let’s see for example, in function createSurfCylinder, you are calling

figure('Position', [10 10 800 800])

clf;
close;

This opens a figure, closes the current figure (that is the figure you have just opened) and closes the current figure (which is now the one that was open before you opened & closed the last figure…

The plot()-function draws a line into the current axis (if not existing, it opens an axis). It can take as a first input a specific axis handle. The default is gca, which is a function meaning something like get current axis.

So to have all control over your plots, keep track of the handles. I always go like this:

fh1 = struct(); % create an empty struct
fh1.fig = figure('Name','very important figure'); % open figure & keep its handle
fh1.ax = subplot(1,1,1); % this opens a single axis & I keep the handle

% plot
plot(   fh.ax, [1,10],[2,5])
ylabel( fh.ax, 'Amplitude Y / unit')
xlabel( fh.ax, 'Measure of time x / unit')

With the command subplot one can arrange multiple axes in one figure (here you need to be aware of the difference between a figure and an axis, and why there are two different types of handles). For example

fh2 = struct(); % create a new empty struct
fh2.fig = figure('Name','Multiple axes in one figure'); % open figure & keep its handle
fh2.ax = cell(3,1);
fh2.ax{1} = subplot(2,2,1); % this opens a single axis & keeps the handle
fh2.ax{2} = subplot(2,2,2); % this opens a single axis & keeps the handle
fh2.ax{3} = subplot(2,2,3:4); % this opens a single axis & keeps the handle

% do plotting with your axis-handles

figure window with 3 axes

(yes, I usually first open all axes and than fill them with graphs/lines)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top