声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 9937|回复: 23

[共享资源] 两个现有的fig图画在一起比较

  [复制链接]
发表于 2009-6-28 20:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

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

  10. % Ckeck the input & output property
  11. PreS = [blanks(10),'*** Error : ']; EndS = ' ! <== Comp2Fig ***'; PreW = [blanks(5),'*** '];
  12. if nargin > 2, error([PreS,'No more 3 input arguments',EndS]); end
  13. if nargout > 0, error([PreS,'No any output arguments',EndS]); end

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

  16. % Begin the figure comparison process
  17. % read the base figure
  18. if isempty(hFig1)
  19.    TitleS = 'Choise the base file for figure combining (Cancel to Quit from Comp2Fig) :';
  20.    beep; [FileName1,PathName] = uigetfile('*.fig',TitleS);
  21.    if FileName1==0, beep; disp([PreW,'File not ready ?',EndS]); return; % abort action
  22.    else cd(PathName); end % change directory
  23.    try, open(FileName1); hFig1 = gcf; catch, return; end;
  24.    DefName = FileName1;
  25. end; hAxes1=get(hFig1,'Children'); pause(0.5);

  26. % read the another figure
  27. if isempty(hFig2)
  28.    TitleS = 'Choise the other file for figure combining (Cancel to Quit from Comp2Fig) :';
  29.    beep; [FileName2,PathName] = uigetfile('*.fig',TitleS);
  30.    if FileName2==0, beep; disp([PreW,'File not ready ?',EndS]); return; % abort action
  31.    else cd(PathName); end % change directory
  32.    try, open(FileName2); hFig2 = gcf; catch, return; end
  33. end; hAxes2=get(hFig2,'Children');

  34. % compare 2 figure
  35. for m = 1:length(hAxes2)
  36.    LColor=get(hAxes1(m),'ColorOrder'); iLine=0;
  37.    hLine1 = get(hAxes1(m),'Children');
  38.    for K=length(hLine1):-1:1, if get(hLine1(K),'Type')=='line', iLine=iLine+1; end; end
  39.    hLine2 = get(hAxes2(m),'Children');
  40.    for K=length(hLine2):-1:1, if get(hLine2(K),'Type')=='line'
  41.       xp = get(hLine2(K),'XData'); yp = get(hLine2(K),'YData'); iLine=iLine+1;
  42.       figure(hFig1); set(hFig1,'CurrentAxes',hAxes1(m)); grid on; hold on;
  43.       hhn=plot(xp,yp); set(hhn,'Color',LColor(mod(iLine,7)+1,:)); axis auto;
  44.    end; end
  45.    xlabel( get( get(hAxes2(m),'XLabel'), 'String') );
  46.    ylabel( get( get(hAxes2(m),'YLabel'), 'String') );
  47. end

  48. % save the combine figure
  49. pause(0.5);
  50. TitleS = 'Filename for saveing plot (Cancel to not save) :';
  51. beep; [FileName3,PathName3] = uiputfile(DefName,TitleS);
  52. if FileName3==0, FileName3=[]; end % abort the save file action
  53. if ~isempty(FileName3),
  54.    try, saveas(hFig1,[PathName3,FileName3]);
  55.    catch, saveas(hFig1,[PathName3,strtok(FileName3,'.')],'fig'); end
  56. end
  57. close(hFig2);

  58. % no output arguments definition
  59. beep; disp([PreW,'Function is complete',EndS]);
  60. return
复制代码


[ 本帖最后由 ChaChing 于 2009-6-28 20:26 编辑 ]
回复
分享到:

使用道具 举报

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

  10. % Ckeck the input & output property
  11. PreS = [blanks(10),'*** Error : ']; EndS = ' ! <== Comp2Fig ***'; PreW = [blanks(5),'*** '];
  12. if nargin > 2, error([PreS,'No more 3 input arguments',EndS]); end
  13. if nargout > 0, error([PreS,'No any output arguments',EndS]); end

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

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

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

  34. % compare 2 figure
  35. for m = 1:length(hAxes2)
  36.    LColor=get(hAxes1(m),'ColorOrder'); iLine=0;
  37.    hLine1 = get(hAxes1(m),'Children');
  38.    for K=length(hLine1):-1:1, if strcmp(get(hLine1(K),'Type'),'line'), iLine=iLine+1; end; end
  39.    hLine2 = get(hAxes2(m),'Children');
  40.    for K=length(hLine2):-1:1, if strcmp(get(hLine2(K),'Type'),'line')
  41.       xp = get(hLine2(K),'XData'); yp = get(hLine2(K),'YData'); iLine=iLine+1;
  42.       figure(hFig1); set(hFig1,'CurrentAxes',hAxes1(m)); grid on; hold on;
  43.       hhn=plot(xp,yp); set(hhn,'Color',LColor(mod(iLine,7)+1,:)); axis auto;
  44.    end; end
  45.    xlabel( get( get(hAxes2(m),'XLabel'), 'String') );
  46.    ylabel( get( get(hAxes2(m),'YLabel'), 'String') );
  47. end

  48. % save the combine figure
  49. pause(0.5);
  50. TitleS = 'Filename for saveing plot (Cancel to not save) :';
  51. beep; [FileName3,PathName3] = uiputfile(DefName,TitleS);
  52. if FileName3==0, FileName3=[]; end % abort the save file action
  53. if ~isempty(FileName3),
  54.    try saveas(hFig1,[PathName3,FileName3]);
  55.    catch, saveas(hFig1,[PathName3,strtok(FileName3,'.')],'fig'); end
  56. end
  57. close(hFig2);

  58. % no output arguments definition
  59. beep; disp([PreW,'Function is complete',EndS]);
  60. return
复制代码
 楼主| 发表于 2011-5-28 16:34 | 显示全部楼层

从一个figure中抓取相关数据

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

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

  21. % Ckeck the input & output property
  22.   PreS = [blanks(10),'*** Error : ']; EndS = ' !  <== PickAxes ***';
  23.   PreW = [blanks(5),'*** '];
  24.   if nargin > 1, error([PreS,'No more 1 input arguments',EndS]); end
  25.   if nargout > 5, error([PreS,'No more 5 output arguments',EndS]); end

  26.   hFig = gcf;  % default - current figure
  27.   if nargin >= 1, hFig = varargin{1}; if isempty(hFig), hFig = gcf; end
  28.      if ~strcmp( get(hFig,'Type'), 'figure')
  29.         error([PreS,'Input argument is not figure handle',EndS]); end
  30.   end

  31. % *** Begin the picking process ***

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

  42.          if hKPos(1)<=PosX & PosX<=hKPos(3) & hKPos(2)<=PosY & PosY<=hKPos(4)
  43.             hAxes = hAxesK(K); set(hFig,'CurrentAxes',hAxes); % axes(hFig);
  44.             hLineK = get(hAxes,'Children'); break; end
  45.      end; end
  46.      if nargout >= 2, if ~isempty(hAxes) & ~isempty(hLineK)
  47.         for K=length(hLineK):-1:1
  48.            if strcmp(get(hLineK(K),'Type'),'line')
  49.               xp = get(hLineK(K),'XData'); yp = get(hLineK(K),'YData');
  50.               try, xData = [xData, xp']; yData = [yData, yp'];  % 96.09.26
  51.               catch, xData = [xData; xp']; yData = [yData; yp']; end
  52.            end
  53.            if strcmp(get(hLineK(K),'Type'),'image')  % 97.11.20
  54.               xData = get(hLineK(K),'XData'); yData = get(hLineK(K),'YData');
  55.               cData = get(hLineK(K),'CData');
  56.            end
  57.         end
  58.      end; end
  59.   end

  60. % output arguments definition
  61.   if nargout >= 1, varargout{1} = hAxes; if nargout >= 2, varargout{2} = xData;
  62.      if nargout >= 3, varargout{3} = yData; if nargout >= 4, varargout{4} = button;
  63.          if nargout >= 5, varargout{5} = cData; end
  64.   end; end; end; end
  65.   return
复制代码
发表于 2011-5-28 22:26 | 显示全部楼层
收藏下!留着以后用!多谢分享
 楼主| 发表于 2011-11-1 01:05 | 显示全部楼层
几个帖问到类似的问题!
顺便交代可能应用时机, 不同参数/试验经烦琐处理完后画出的结果档(*.fig), 仅需存此档, 日后即可进行比较或后续处理!
发表于 2011-11-11 21:46 | 显示全部楼层
回复 3 # ChaChing 的帖子

你好,我怎么提取不出数据啊
 楼主| 发表于 2011-11-12 02:03 | 显示全部楼层
回复 6 # wypzf_8 的帖子

LS怎使用? 可否说清楚些!
有无错误讯息!?
发表于 2011-11-12 08:53 | 显示全部楼层
回复 7 # ChaChing 的帖子

我使用下面命令
x=0:0.01:2*pi;y=sin(x);plot(x,y);再运行m文件,(我把上面的代码已经存在m文件下),在图片上出现一个十字的图标,单击某一点,结果没得到数据。
 楼主| 发表于 2011-11-12 10:58 | 显示全部楼层
回复 8 # wypzf_8 的帖子

刚再试下了, 没问题啊
LS所谓"再运行m文件"是什麼意思?
发表于 2011-11-13 18:06 | 显示全部楼层
回复 9 # ChaChing 的帖子

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

步骤1.jpg
我的操作步骤2.jpg
发表于 2011-11-13 18:12 | 显示全部楼层
本帖最后由 wypzf_8 于 2011-11-13 18:15 编辑

回复 9 # ChaChing 的帖子

楼主,十分感谢你的回帖,你在回帖中使用LS,是指matlab的一个命令,还是有其他含义?你在两次回帖中都用到了LS,第一次我以为是命令,第二次我以为是论坛上的会员,看了整个帖子,也无此人啊;那应该代表某种含义啊,经过百度知道楼上的意思,看来我落伍了啊,呵呵
 楼主| 发表于 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);
[hAxes,xData,yData,button,cData] = PickAxes(hh)
发表于 2011-11-14 09:02 | 显示全部楼层
回复 12 # ChaChing 的帖子

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

正常运行

正常运行

显示错误

显示错误
直接粘贴到命令窗口.jpg
 楼主| 发表于 2011-11-14 10:57 | 显示全部楼层
回复 13 # wypzf_8 的帖子

非常抱歉, 应该是这样, 12F贴错了
x=0:0.01:2*pi; y=sin(x); plot(x,y);
[hAxes,xData,yData,button,cData] = PickAxes(gcf)
发表于 2011-11-14 15:16 | 显示全部楼层
回复 14 # ChaChing 的帖子

谢谢楼主,楼主,我操作成功了。楼主,你很厉害,我认你当师傅吧,我就是个matlab小白
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-15 23:00 , Processed in 0.083238 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表