Plotting in Matlab
How do you plot two figures at the same time in Matlab? Every time I use surf() it plots over the old one. Also, how do you save the images so you can expo开发者_JAVA技巧rt them to MS word or powerpoint or something?
You can plot two figures in separate windows:
figure(1)
% do plotting
figure(2)
% do plotting
or in subplots:
figure(1)
subplot(1, 2, 1)
% do plotting
subplot(1, 2, 2)
% do plotting
For more info, you can see the MATLAB docs for the figure and subplot functions (in the help menu).
For printing the images to a file, see the documentation for the print function. Or just go to File -> Save As, and pick the image type you want.
Use the command figure before each plot/surf/mesh.
example
X = [1:5];
figure('Name', 'My plot');
plot(X, X+X);
figure('Name', 'My plot number 2');
plot(X, X + X + X);
Call figure
before calling surf
. figure
opens a new figure window. When you call surf
, it will plot into the currently selected figure.
You can copy-paste figures into Word or Powerpoint by using, in the figure window, the menu Edit->Copy Figure. If in, say Word, you click on the pasted figure and select 'ungroup', you can even go and edit the figure.
To save, you select 'Save as...' in the File-menu in the figure window. For Adobe Illustrator, save as .eps (works better than .ai).
As another small addition to previous responses, you can print a figure directly to clipboard using print -dmeta
command. Then just paste to Word or PowerPoint document. I found it very neat.
@kwatford If you use hold all
rather than hold on
then Matlab will use the next defined colour and linestyle for that plot. check out the difference between
figure(1);
plot(rand(100,1));
hold on ;
plot(rand(100,1)+2);
and
figure(2);
plot(rand(100,1));
hold all;
plot(rand(100,1)+2);
To create a new figure in a separate window, just say figure
. To export as an image file, use the print
command with the appropriate -d
option to select the file format. Like so:
figure;
plot(rand(100,1), rand(100, 1), 'r*');
print -dpng 'MyImage.png'
Execute hold on
to hold the current figure. New plots will be added to the existing plots. Use hold off
to change it back to the previous behavior.
In addition to the print
command (see Drew Hall's response), you can export to other formats via the File menu, or use the Copy Figure function of the edit menu. If you want to paste it into Word or Powerpoint, you might get a better result if you use "Paste Special" instead of normal Paste.
精彩评论