MATLAB toolbox(1)

2017-01-16  by:CAE仿真在線  來源:互聯(lián)網(wǎng)

以下函數(shù)自編;主要用于在一個figure上畫多個類似的圖,各小圖之間橫軸縱軸范圍要近的圖。



實現(xiàn)功能如下圖所示

[h,ax,bigax]=multigrid('newfig',4,2);

在一個figure生成4行2列的axis

MATLAB toolbox(1)

分別在每個axis上畫圖

x = randn(100,4,2);

for i=1:4
for j =1:2
set(h,'CurrentAxes',ax(i,j),'NextPlot','add');
stairs(x(:,i,j));
axis tight;

end
end


MATLAB toolbox(1)

去掉各axis橫縱軸,只留最下,最左坐標格
multigrid('adjust',ax)


MATLAB toolbox(1)


為每個小圖標記

numlab = {'a','b';
'c','d';
'e','f';
'g','h'}
multigrid('label',ax,numlab);

MATLAB toolbox(1)



xlabel(bigax,'time');ylabel(bigax,'amplitude')

title(bigax,'signals')

MATLAB toolbox(1)


===============源程序===============================


function varargout=multigrid(flag,varargin)
% MULTIPLOT
% [H,AX,BIGAX]=MULTIPLOT(ROWS,COLS,LABEL,FUNHANDLE)
%
% [h,ax,bigax]=multigrid(flag,varargin)
%
% [h,ax,bigax]=multigrid('newfig',rows,cols)
% multigrid('label',ax,numlabel)
% multigrid('adjust',ax)
% multigrid('add',ax,codes,varargin);
%
% flag:
% newfig, creat new figure with the setting rows and cols.
% label, add the label on the figure
% adjust, adjust the x and y axes limit
%
%
% Author: J.H.Zhu<jhzhu99@gmail.com>
% Date: April 06, 2011.
% Revised:
% Revised:


switch flag
case {'newfig'}
rows =[]; cols =[];
if nargin>1,rows=varargin{1};end
if nargin>2, cols=varargin{2};end

if isempty(rows),rows =1; end
if isempty(cols),cols =1; end

h = clf;
BigAx = newplot;

set(BigAx,'Visible','off','color','none');

ax =zeros(rows,cols);
pos = get(BigAx,'Position');
width = pos(3)/cols;
height = pos(4)/rows;
space = .05; % 3 percent space between axes
pos(1:2) = pos(1:2) + space*[width height];
for i=rows:-1:1,
for j=cols:-1:1,
axPos = [pos(1)+(j-1)*width pos(2)+(rows-i)*height ...
width*(1-space) height*(1-space)];
ax(i,j) = axes('Position',axPos,'parent',h,...
'visible', 'on', 'Box','on');
end
end
idx = false(rows,cols);idx(rows,:)=1;
set(ax(~idx),'xticklabel','');
idx = false(rows,cols);idx(:,1)=1;
set(ax(~idx),'yticklabel','');

% Also set Title and X/YLabel visibility to on and strings to empty
set([get(BigAx,'Title'); get(BigAx,'XLabel'); get(BigAx,'YLabel')], ...
'String','','Visible','on');

varargout ={h,ax,BigAx};
case {'label'}
ax =[]; label=[];
if nargin>1,ax = varargin{1}; end
if nargin>2,label=varargin{2}; end

if isempty(ax),return; end
if isempty(label),return;end

[rows,cols]=size(ax);
for i =rows:-1:1
for j =cols:-1:1
if isnan(ax(i,j)),xlim(i,j,1:2)=NaN; ylim(i,j,1:2)=NaN; continue;end
set(get(ax(i,j),'parent'),'CurrentAxes',ax(i,j));
xlim(i,j,:)=get(ax(i,j),'xlim');
ylim(i,j,:)=get(ax(i,j),'ylim');
if ~isempty(label)
if isnumeric(label{i,j}),tex = num2str(label{i,j});
else tex =label{i,j};end
if ~all(isnan(tex))||~isempty(tex)
text(xlim(i,j,2),ylim(i,j,1),['\bf',tex],...
'HorizontalAlignment','center',...
'VerticalAlignment','baseline',...
'BackgroundColor',[0.992 0.918 0.796],...
'Interpreter','tex');
end
end
end
end

case {'adjust'}
ax =[];
if nargin>1,ax=varargin{1};end
if isempty(ax),return;end

[rows,cols]=size(ax);
for i =rows:-1:1
for j =cols:-1:1
if isnan(ax(i,j))
xlim(i,j,:)=NaN; ylim(i,j,:)=NaN;
continue;
end
set(get(ax(i,j),'parent'),'CurrentAxes',ax(i,j));
xlim(i,j,:)=get(ax(i,j),'xlim');
ylim(i,j,:)=get(ax(i,j),'ylim');
end
end

xlimmin =min(xlim(:,:,1),[],1); xlimmax =max(xlim(:,:,2),[],1);
ylimmin =min(ylim(:,:,1),[],2); ylimmax =max(ylim(:,:,2),[],2);

inset = .02;
for i=1:rows,
if isnan(ax(i,1)),continue;end
set(ax(i,1),'ylim',[ylimmin(i,1) ylimmax(i,1)])
dy = diff(get(ax(i,1),'ylim'))*inset;
idx = isnan(ax(i,:));
set(ax(i,~idx),'ylim',[ylimmin(i,1)-dy ylimmax(i,1)+dy])
end
for j=1:cols
if isnan(ax(1,j)),continue;end
set(ax(1,j),'xlim',[xlimmin(1,j) xlimmax(1,j)])
dx = diff(get(ax(1,j),'xlim'))*inset;
idx = isnan(ax(:,j));
set(ax(~idx,j),'xlim',[xlimmin(1,j)-dx xlimmax(1,j)+dx])
end
idx = findemptyidx('xtick',ax);
set(ax(~idx),'xticklabel','')
idx = findemptyidx('ytick',ax);
set(ax(~idx),'yticklabel','')
case {'add'}
ax =[];
if nargin>1,ax=varargin{1};end
if nargin>2,codes = varargin{2};end
if isempty(ax),return;end
if isempty(codes);return;end

[rows,cols]=size(ax);
for i =rows:-1:1
for j =cols:-1:1
if isnan(ax(i,j)),continue;end
set(get(ax(i,j),'parent'),'CurrentAxes',ax(i,j));
eval_r(codes);
end
end


otherwise
return;
end



function idx = findemptyidx(axesname,ax)

switch axesname

case {'xtick'}
rows=size(ax,1);
idx = isnan(ax);
[m,n]=find(idx==1);
a = m-1; a(a<1)=1;
b = m+1; b(b>rows)=rows;

idx(a,n)=1; idx(b,n)=1;
idx(end,:)=1;

case {'ytick'}
cols=size(ax,2);
idx = isnan(ax);
[m,n]=find(idx==1);
a = n-1; a(a<1)=1;
b = n+1; b(b>cols)=cols;

idx(m,a)=1; idx(m,b)=1;
idx(:,1)=1;

end


開放分享:優(yōu)質(zhì)有限元技術(shù)文章,助你自學成才

相關(guān)標簽搜索:MATLAB toolbox(1) MatLab培訓 MatLab培訓課程 MatLab在線視頻教程 MatLab技術(shù)學習教程 MatLab軟件教程 MatLab資料下載 MatLab代做 MatLab基礎(chǔ)知識 Fluent、CFX流體分析 HFSS電磁分析 Ansys培訓 Abaqus培訓 

編輯
在線報名:
  • 客服在線請直接聯(lián)系我們的客服,您也可以通過下面的方式進行在線報名,我們會及時給您回復電話,謝謝!
驗證碼

全國服務(wù)熱線

1358-032-9919

廣州公司:
廣州市環(huán)市中路306號金鷹大廈3800
電話:13580329919
          135-8032-9919
培訓QQ咨詢:點擊咨詢 點擊咨詢
項目QQ咨詢:點擊咨詢
email:kf@1cae.com