MATLAB 操作总结
如何用命令行保存图片?
- saveas
用法:saveas( fig, filename, formattype )
Example:
x = [2 4 5 6 7];
bar(x);
saveas( gcf, 'Barchart.png' )
saveas( gcf, 'Barchart', 'epsc' ) % save figure as EPS file
用法: print( filename, formattype, formatoptions )
Example:
% save figure as image file
bar(1:10)
print( 'BarPlot', '-dpng' )
% copy figure to Clipboard
plot(1:10)
print( '-clipboard','dmeta' )
fig = figure;
plot(1:10);
print( fig, 'MySaveplot', '-dpng' )
画图控制
用到的重要函数有 gca, gcf, set, text
。
在需要的时候请仔细阅读 MATLAB 中 Axes Properties
等相关文档,了解图形控制中可变参数。
函数说明:
-
gca
返回当前坐标轴和图形。x = -2*pi:0.1:2*pi; y = sin(x); plot(x,y) xlabel('x') ylabel('y') ax = gca; ax.XAxisLocation ax.YAxisLocation ax.XAxisLocation = 'origin'; % setting x axis location to origin ax.YAxisLocation = 'origin'; % setting y axis location to origin ax.Box = 'off'; % Turn off the display of the axes outline ax.Layer = 'top'; % Make grid lines and tick marks appear over graphics objects
-
gcf
返回当前图形的句柄surf(peaks) fig = gcf; fig.Color = [0 0.5 0.5]; fig.ToolBar = 'none';
-
set
设置图形目标性质语法:
set(H, Name, Value)
x = 0:30; y = [1.5*cos(x); 4*exp(-.1*x).*cos(x); exp(.05*x).*cos(x)]'; S = stem(x,y); NameArray = {'Marker','Tag'}; ValueArray = {'o','Decaying Exponential';... 'square','Growing Exponential';... '*','Steady State'}; set(S,NameArray,ValueArray)
子图控制
Colorbar控制
Position 参数为 [left bottom width height]
x=[60:20:260]; %set x axis ticks
y=rand(11); %get something to plot
h1=subplot(2,1,2); %setup subplot1
plot(x,y,'-.'); %plot subplot1
box on % leave only x and y axes
xlim([60 260]) %setup some x axis
set(h1,'Xtick',x) %set the x axis ticks to show only x
h1_pos = get(h1,'Position'); %get the position data for sublot1.
y2 = 10*y.^2; %make something up for subplot2
h2=subplot(2,1,1); %make subplot2
plot(x,10*y,'-.'); %plot subplot2
box on
set(h2,'Xcolor',[1 1 1]) %make the Y axis line white
set(h2,'Xtick',[])
xlim([60 260]) %setup some x axis
h2_pos=get(h2,'Position');
set(h2,'Position',[h1_pos(1) h1_pos(2)+h1_pos(4) h2_pos(3:end)]) %using position of subplot1 put subplot2next to it.
坐标轴控制
-
坐标轴刻度朝外
set(gca, 'tickdir', 'out')
-
坐标轴颜色
set(gca, 'XColor', 'red')
-
其它
set(gca, 'XLim', [min max]); % range set(gca, 'XTick', [ markerpoint ]); % markerpoint set(gca, 'XTicklabel', {}]; %marker set(gca, 'XGrid', 'on'); %grid on
防止图缩小后字体过小或发虚
set(gcf, 'Position', [100 100 260 220]);
set(gca, 'Position', [.13 .17 .80 .74]);
figure_Fontsize = 8;
set(get(gca, 'XLabel'), 'FontSize', figure_Fontsize, 'Vertical', 'top');
set(get(gca, 'YLabel'), 'FontSize', figure_Fontsize, 'Vertical', 'middle');
set(findobj('FontSize',10), 'FontSize', figure_Fontsize);
set(findobj(get(gca, 'Children'), 'LineWidth', 0.5), 'LineWidth', 2);
图形窗口
colordef
定义绘图背景色
随机数生成
可用函数: rand, randi, randn, randperm, randsrc, unifrnd