ChaChing 发表于 2009-6-28 20:14

两个现有的fig图画在一起比较

本想直接回覆http://forum.vibunion.com/thread-83796-1-1.html, 但不确定是否对题! 且不太会搜索的我, 想想个人日後找寻方便, 就直接发帖!
这是几年前, 处理一堆试验资料时, 为方便比较不同案件间的差异, 应急临时写的!
基本上, 两图都可各有多条线! 若有错误亦请告知!
function varargout = Comp2Fig(varargin)
% Comp2Fig - Combine 2 figure for comparison.
%
% Comp2Fig or Comp2Fig(hFig1,hFig2)
%
% hFig1 : input figure handle for base (empty for user selection)
% hFig2 : input figure handle for other (empty for user selection)
% *** No output arguments, input is optinal select
%

% Ckeck the input & output property
PreS = ; EndS = ' ! <== Comp2Fig ***'; PreW = ;
if nargin > 2, error(); end
if nargout > 0, error(); end

hFig1 = []; hFig2 = []; DefName = '';
if nargin >= 1, hFig1 = varargin{1}; if nargin >= 2, hFig2 = varargin{2}; end; end

% Begin the figure comparison process
% read the base figure
if isempty(hFig1)
   TitleS = 'Choise the base file for figure combining (Cancel to Quit from Comp2Fig) :';
   beep; = uigetfile('*.fig',TitleS);
   if FileName1==0, beep; disp(); return; % abort action
   else cd(PathName); end % change directory
   try, open(FileName1); hFig1 = gcf; catch, return; end;
   DefName = FileName1;
end; hAxes1=get(hFig1,'Children'); pause(0.5);

% read the another figure
if isempty(hFig2)
   TitleS = 'Choise the other file for figure combining (Cancel to Quit from Comp2Fig) :';
   beep; = uigetfile('*.fig',TitleS);
   if FileName2==0, beep; disp(); return; % abort action
   else cd(PathName); end % change directory
   try, open(FileName2); hFig2 = gcf; catch, return; end
end; hAxes2=get(hFig2,'Children');

% compare 2 figure
for m = 1:length(hAxes2)
   LColor=get(hAxes1(m),'ColorOrder'); iLine=0;
   hLine1 = get(hAxes1(m),'Children');
   for K=length(hLine1):-1:1, if get(hLine1(K),'Type')=='line', iLine=iLine+1; end; end
   hLine2 = get(hAxes2(m),'Children');
   for K=length(hLine2):-1:1, if get(hLine2(K),'Type')=='line'
      xp = get(hLine2(K),'XData'); yp = get(hLine2(K),'YData'); iLine=iLine+1;
      figure(hFig1); set(hFig1,'CurrentAxes',hAxes1(m)); grid on; hold on;
      hhn=plot(xp,yp); set(hhn,'Color',LColor(mod(iLine,7)+1,:)); axis auto;
   end; end
   xlabel( get( get(hAxes2(m),'XLabel'), 'String') );
   ylabel( get( get(hAxes2(m),'YLabel'), 'String') );
end

% save the combine figure
pause(0.5);
TitleS = 'Filename for saveing plot (Cancel to not save) :';
beep; = uiputfile(DefName,TitleS);
if FileName3==0, FileName3=[]; end % abort the save file action
if ~isempty(FileName3),
   try, saveas(hFig1,);
   catch, saveas(hFig1,,'fig'); end
end
close(hFig2);

% no output arguments definition
beep; disp();
return


[ 本帖最后由 ChaChing 于 2009-6-28 20:26 编辑 ]

ChaChing 发表于 2011-3-12 14:32

忘记什麼状况会出错, 反正这个已修正如下!
说明下, 不同fig之间需要有相似性, 如fig1有2*3子图, 则fig2即需有2*3子图
还有许多地方并非最佳写法, 个人坏习惯, 可用就懒得改!
当然若有错误亦请告知!function Comp2Fig(varargin)
% Comp2Fig - Combine 2 figure for comparison.
%
% Comp2Fig or Comp2Fig(hFig1,hFig2)
%
% hFig1 : input figure handle for base (empty for user selection)
% hFig2 : input figure handle for other (empty for user selection)
% *** No output arguments, input is optinal select
%

% Ckeck the input & output property
PreS = ; EndS = ' ! <== Comp2Fig ***'; PreW = ;
if nargin > 2, error(); end
if nargout > 0, error(); end

hFig1 = []; hFig2 = []; DefName = '';
if nargin >= 1, hFig1 = varargin{1}; if nargin >= 2, hFig2 = varargin{2}; end; end

% Begin the figure comparison process
% read the base figure
if isempty(hFig1)
   TitleS = 'Choise the base file for figure combining (Cancel to Quit from Comp2Fig) :';
   beep; = uigetfile('*.fig',TitleS);
   if FileName1==0, beep; disp(); return; % abort action
   else cd(PathName); end % change directory
   try open(FileName1); hFig1 = gcf; catch, disp(['Open File Error!',EndS]); return; end;
   DefName = FileName1;
end; hAxes1=get(hFig1,'Children'); pause(0.5);

% read the another figure
if isempty(hFig2)
   TitleS = 'Choise the other file for figure combining (Cancel to Quit from Comp2Fig) :';
   beep; = uigetfile('*.fig',TitleS);
   if FileName2==0, beep; disp(); return; % abort action
   else cd(PathName); end % change directory
   try open(FileName2); hFig2 = gcf; catch, disp(['Open File Error!',EndS]); return; end
end; hAxes2=get(hFig2,'Children');

% compare 2 figure
for m = 1:length(hAxes2)
   LColor=get(hAxes1(m),'ColorOrder'); iLine=0;
   hLine1 = get(hAxes1(m),'Children');
   for K=length(hLine1):-1:1, if strcmp(get(hLine1(K),'Type'),'line'), iLine=iLine+1; end; end
   hLine2 = get(hAxes2(m),'Children');
   for K=length(hLine2):-1:1, if strcmp(get(hLine2(K),'Type'),'line')
      xp = get(hLine2(K),'XData'); yp = get(hLine2(K),'YData'); iLine=iLine+1;
      figure(hFig1); set(hFig1,'CurrentAxes',hAxes1(m)); grid on; hold on;
      hhn=plot(xp,yp); set(hhn,'Color',LColor(mod(iLine,7)+1,:)); axis auto;
   end; end
   xlabel( get( get(hAxes2(m),'XLabel'), 'String') );
   ylabel( get( get(hAxes2(m),'YLabel'), 'String') );
end

% save the combine figure
pause(0.5);
TitleS = 'Filename for saveing plot (Cancel to not save) :';
beep; = uiputfile(DefName,TitleS);
if FileName3==0, FileName3=[]; end % abort the save file action
if ~isempty(FileName3),
   try saveas(hFig1,);
   catch, saveas(hFig1,,'fig'); end
end
close(hFig2);

% no output arguments definition
beep; disp();
return

ChaChing 发表于 2011-5-28 16:34

从一个figure中抓取相关数据

这个以前上传过,http://forum.vibunion.com/thread-72970-1-1.html
今天被翻上来, 但有更新过, 感觉摆在这裡一起比较好function varargout = PickAxes(varargin)
% PickAxes    - Pick one axes handle from figure. (91.11.06, JCK)
%
% = PickAxes(hFig)
%
%      hFig : figure handle (default : gcf)
%   hAxes : picked axes handle from hFig figure
%   xData : x coordinate of line in axes which select by mouse
%   yData : y coordinate of line in axes which select by mouse
%   cData : image data in axes which select by mouse
%    button : mouse button id
%
%    *** input/output is optinal select. if no output argument, pick axes only.
%    *** xData/yData may be more column if the selected axes have more lines.
%
%    See also LOOPHP, STRIPPLOT, ORBITPLOT.

%92.03.29 - include comment and I/O check & modify some logic.
%92.08.19 - modify the logic for using beep to match V6.5.
%92.12.16 - add legend check & change 'axes' to 'set' type & reverse x/y data.
%96.09.26 - modify for different point line case.

% Ckeck the input & output property
PreS = ; EndS = ' !<== PickAxes ***';
PreW = ;
if nargin > 1, error(); end
if nargout > 5, error(); end

hFig = gcf;% default - current figure
if nargin >= 1, hFig = varargin{1}; if isempty(hFig), hFig = gcf; end
   if ~strcmp( get(hFig,'Type'), 'figure')
      error(); end
end

% *** Begin the picking process ***

figure(hFig); PsF = get(gcf,'Position'); hAxes = []; xData = []; yData = []; cData = [];
set(0,'PointerLocation',); = ginput(1);
if isempty(button), button = 99; end% Keyboard Enter
if button == 2, pause; end% Selection for pause
if button == 1 | button == 3, CpF = get(gcf,'CurrentPoint');
   PosX=CpF(1)/PsF(3); PosY=CpF(2)/PsF(4); hAxesK = get(hFig,'Children');
   for K = 1:length(hAxesK)
   if strcmp(get(hAxesK(K),'Type'),'axes') & ~strcmp(get(hAxesK(K),'Tag'),'legend')
         hKPos = get(hAxesK(K),'Position');
         hKPos(3) = hKPos(1) + hKPos(3); hKPos(4) = hKPos(2) + hKPos(4);

         if hKPos(1)<=PosX & PosX<=hKPos(3) & hKPos(2)<=PosY & PosY<=hKPos(4)
            hAxes = hAxesK(K); set(hFig,'CurrentAxes',hAxes); % axes(hFig);
            hLineK = get(hAxes,'Children'); break; end
   end; end
   if nargout >= 2, if ~isempty(hAxes) & ~isempty(hLineK)
      for K=length(hLineK):-1:1
         if strcmp(get(hLineK(K),'Type'),'line')
            xp = get(hLineK(K),'XData'); yp = get(hLineK(K),'YData');
            try, xData = ; yData = ;% 96.09.26
            catch, xData = ; yData = ; end
         end
         if strcmp(get(hLineK(K),'Type'),'image')% 97.11.20
            xData = get(hLineK(K),'XData'); yData = get(hLineK(K),'YData');
            cData = get(hLineK(K),'CData');
         end
      end
   end; end
end

% output arguments definition
if nargout >= 1, varargout{1} = hAxes; if nargout >= 2, varargout{2} = xData;
   if nargout >= 3, varargout{3} = yData; if nargout >= 4, varargout{4} = button;
         if nargout >= 5, varargout{5} = cData; end
end; end; end; end
return

wangyouyi 发表于 2011-5-28 22:26

收藏下!留着以后用!多谢分享{:{39}:}

ChaChing 发表于 2011-11-1 01:05

几个帖问到类似的问题!
顺便交代可能应用时机, 不同参数/试验经烦琐处理完后画出的结果档(*.fig), 仅需存此档, 日后即可进行比较或后续处理!

wypzf_8 发表于 2011-11-11 21:46

回复 3 # ChaChing 的帖子

你好,我怎么提取不出数据啊

ChaChing 发表于 2011-11-12 02:03

回复 6 # wypzf_8 的帖子

LS怎使用? 可否说清楚些!
有无错误讯息!?

wypzf_8 发表于 2011-11-12 08:53

回复 7 # ChaChing 的帖子

我使用下面命令
x=0:0.01:2*pi;y=sin(x);plot(x,y);再运行m文件,(我把上面的代码已经存在m文件下),在图片上出现一个十字的图标,单击某一点,结果没得到数据。

ChaChing 发表于 2011-11-12 10:58

回复 8 # wypzf_8 的帖子

刚再试下了, 没问题啊
LS所谓"再运行m文件"是什麼意思?

wypzf_8 发表于 2011-11-13 18:06

回复 9 # ChaChing 的帖子

“再运行m文件”就是获取数据啊,该m文件就是从该贴粘贴复制过去的。
我先用plot画一个图,再使用你帖子的命令获取数据,可是没有显示我获得的数据啊,也没有在命令行里提示错误信息,说明该命令对着呢,我是个小白,刚学matlab时间不长,我把我的操作附上,你帮我看看哪儿出问题了,谢谢

wypzf_8 发表于 2011-11-13 18:12

本帖最后由 wypzf_8 于 2011-11-13 18:15 编辑

回复 9 # ChaChing 的帖子

楼主,十分感谢你的回帖,你在回帖中使用LS,是指matlab的一个命令,还是有其他含义?你在两次回帖中都用到了LS,第一次我以为是命令,第二次我以为是论坛上的会员,看了整个帖子,也无此人啊;那应该代表某种含义啊,经过百度知道楼上的意思,看来我落伍了啊,呵呵

ChaChing 发表于 2011-11-13 22:01

回复 10 # wypzf_8 的帖子

看了你的附图, 证明我先前的怀疑, 这也是我想问清楚"再运行m文件"是什麼意思的目的!
建议LS在command win下help PickAxes, 或看下PickAxes前面几行说明!
LS直接执行该函数, 相当下PickAxes(gcf), 当然无任何返回资料!
试试在command win以下命令看看吧!
x=0:0.01:2*pi; y=sin(x); hh=plot(x,y);
= PickAxes(hh)

wypzf_8 发表于 2011-11-14 09:02

回复 12 # ChaChing 的帖子

十分感谢楼主的耐心回复,我按着楼主的提示,操作了一遍,还是提示错误,说输入参数问题,我是笨到家的小白,我把我的操作再次附上,希望楼主帮我看看,谢谢。图1运行正常,图2提示错误,图3直接粘贴命令流到里面运行,提示定义错误。估计我是这个网站最小白的一个matlab新手了。

ChaChing 发表于 2011-11-14 10:57

回复 13 # wypzf_8 的帖子

非常抱歉, 应该是这样, 12F贴错了
x=0:0.01:2*pi; y=sin(x); plot(x,y);
= PickAxes(gcf)

wypzf_8 发表于 2011-11-14 15:16

回复 14 # ChaChing 的帖子

谢谢楼主,楼主,我操作成功了。楼主,你很厉害,我认你当师傅吧,我就是个matlab小白
页: [1] 2
查看完整版本: 两个现有的fig图画在一起比较