找回密码
 注册
Simdroid-非首页
查看: 289|回复: 6

【原创】两个小工具-----备份,统计。我的matlab文件夹有184万行代码,你的呢?

[复制链接]
发表于 2009-10-2 14:45:11 | 显示全部楼层 |阅读模式 来自 重庆
本帖最后由 messenger 于 2009-10-2 16:04 编辑

有很多程序开发的IDE,都有丰富的工具。
而matlab,备份代码的工具没有,版本管理的工具也需要额外安装。基本的统计代码的功能也缺乏。



我以前做了个 统计代码的程序,发现有缺陷。
现在,我重新修改了程序,统计起来,效率还是不错。
可以获得,你指定的文件夹内,有多少个 子目录,多少个文件,有多少行代码,有多少个字符

我自己的matlab,测试结果,如下图
  1. function myCount(varargin)
  2. %by amen@vip.sina.com
  3. targetDir = 0;
  4. while targetDir==0
  5.     targetDir = uigetdir(pwd);
  6. end
  7. nLines=subMyCount(targetDir);
  8. disp('--');
  9. disp('Task is finished');
  10. disp(['Total folders of your project is: ' num2str(nLines(3), '%10.0f') '  folders']);
  11. disp(['Total files of your project is: ' num2str(nLines(2), '%10.0f') '  files']);
  12. disp(['Total lines of your project is: ' num2str(nLines(1), '%10.0f') '  lines']);
  13. disp(['Total elements  of your project is: ' num2str(nLines(4), '%15.0f') '  elements ']);
  14. end

  15. function valLine=subMyCount(curDirStr)
  16. dirfiles = dir(curDirStr);
  17. dLen=length(dirfiles);
  18. flagDir00=zeros(dLen+5,1);
  19. for kk=1:1:dLen
  20.     switch dirfiles(kk).name
  21.         case '..'
  22.             flagDir00(1)=kk;
  23.         case '.'
  24.             flagDir00(2)=kk;
  25.         case 'System Volume Information' %这里,对于Windows,要排除分区根目录下的几个系统文件夹
  26.             flagDir00(3)=kk;
  27.         case 'RECYCLER'
  28.             flagDir00(4)=kk;
  29.         case 'Recycled'
  30.             flagDir00(5)=kk;
  31.         otherwise
  32.             if ~dirfiles(kk).isdir
  33.                 flagDir00(kk+5)=kk;
  34.             end
  35.     end
  36. end
  37. if flagDir00(1)~=0
  38.     flagDir00(3:5)=[];
  39. end
  40. flagDir00(flagDir00==0)=[];
  41. dirfiles(flagDir00)=[];
  42. dLen=length(dirfiles);
  43. nLines=subCount(curDirStr);
  44. if dLen~=0
  45.     for kk=1:1:dLen
  46.         subDirStr=fullfile(curDirStr,dirfiles(kk).name);
  47.         nLines=nLines+subMyCount(subDirStr);
  48.     end
  49. end
  50. valLine=nLines;
  51. end

  52. function valLine=subCount(curDirStr)
  53. disp('Searching Directory:');
  54. disp(['    ' curDirStr]);
  55. nLines=0;
  56. nLen=0;
  57. nMem=0;
  58. subDirStr=[curDirStr filesep '*.m'];
  59. flagNull=dir(subDirStr);
  60. if ~isempty(flagNull)
  61.     nLen=length(flagNull);
  62.     for k=1:1:nLen
  63.         fid = fopen(fullfile(curDirStr,flagNull(k).name),'r');
  64.         [tline,tMem] = fread(fid);
  65.         nLCount = find(tline==10);
  66.         nMem=nMem+tMem;
  67.         nLines=nLines+length(nLCount)+1;
  68.         fclose(fid);
  69.     end
  70. end
  71. valLine(1)=nLines;
  72. valLine(2)=nLen;
  73. valLine(3)=1;
  74. valLine(4)=nMem;
  75. end
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

1

查看全部评分

 楼主| 发表于 2009-10-2 14:50:22 | 显示全部楼层 来自 重庆
Simdroid开发平台
有时候,我们要做一个 几万行代码的matlab 程序,每天,最好都有个备份。
因此,我做了个程序,用于备份。
这个程序,myBackUp(Pwd),就可以备份当前目录下的所有子目录内的程序代码,到指定的文件夹
当然,也可以传递其它的路径,来备份。






  1. function myBackUp(curPwd)
  2. disp('Begin BackUP');
  3. try
  4.     savefile = fullfile(pwd, 'Project_Work','SaveData','Config01.mat');
  5.     tmpVar01=load(savefile, 'dName');
  6.     backPath = tmpVar01.dName;
  7.     disp('Current BackUp Directory is:');
  8.     disp(['      ' backPath]);
  9.     disp('Do you want to Change BackUp Directory');
  10.     reply = input('1.change\n【2】.no change\nChoose:\n', 's');
  11. catch
  12.     reply='1';
  13. end
  14. if reply=='1'
  15.     dName = 0;
  16.     while dName== 0
  17.         dName = uigetdir(pwd,'Select Data Path');
  18.     end
  19.     backPath = dName;
  20.     if isempty(dir(fullfile(pwd, 'Project_Work','SaveData')))
  21.         mkdir(fullfile(pwd, 'Project_Work','SaveData'));
  22.     end
  23.     try
  24.         save(savefile,'dName','-append');
  25.     catch
  26.         save(savefile,'dName');
  27.     end
  28.     disp('BackUp Directory is :');
  29.     disp(dName);
  30.     disp('Change BackUp Directory OK!');
  31. end
  32. disp('------------------------');
  33. disp('Do you want to del .p files before BackUP:');
  34. reply = input('1.del\n【2】.no change\nChoose:\n', 's');
  35. if reply=='1'
  36.     disp('------------------------');
  37.     myPcode(2);
  38. end
  39. reply = input('Want some Prefix ? \n', 's');
  40. dest= datestr(now, 'yyyymmddTHHMMSS');
  41. if length(reply)>1
  42.     dest=[reply '_' dest];
  43. end
  44. disp('------------------------');
  45. [status,message,messageid] =mkdir(backPath,dest);
  46. if status==1
  47.     disp('Creat directory successful.');
  48.     disp([message messageid]);
  49. else
  50.     disp('Creat directory failed.');
  51.     disp([message messageid]);
  52.     return;
  53. end
  54. destdir=fullfile(backPath,dest);
  55. [status,message,messageid]=copyfile(curPwd,destdir);
  56. if status==1
  57.     disp('Copy files successful.');
  58.     disp([message messageid]);
  59. else
  60.     disp('Copy files failed.');
  61.     disp([message messageid]);
  62.     return;
  63. end
  64. disp(['Source directory is: '  char(10) '    ' curPwd]);
  65. disp(['Destination directory is: '   char(10) '    ' destdir]);
  66. end
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
回复 不支持

使用道具 举报

发表于 2009-10-2 15:14:03 | 显示全部楼层 来自 陕西西安
收下备用,呵呵~!
回复 不支持

使用道具 举报

发表于 2009-10-2 20:34:24 | 显示全部楼层 来自 上海浦东新区
统计行数如果简单,用WC就可以,如果要求高,SLOC可以统计去除注释后的代码行数。
备份最好的办法是用专业些的工具,如CVS、SVN,如果希望有商业支持,Perforce只要$450,如果有钱,ClearCase是最好的选择。

做这样的程序练手可以,发布是没有必要的。这类工作应该用专业工具。

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2009-10-3 11:24:26 | 显示全部楼层 来自 浙江杭州
4# echo_her

本版是Matlab版,不是应用软件版,是讨论Matlab,相互学习、提高的地方,lz的代码可以很好地展示Matlab一些命令的用法、功能,这就足够了。

你的贴子让我想起前些日子那个“matlab曲线拟合与origin没法比”的贴子。

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2009-10-3 15:12:33 | 显示全部楼层 来自 黑龙江哈尔滨
统计行数如果简单,用WC就可以,如果要求高,SLOC可以统计去除注释后的代码行数。
备份最好的办法是用专业些的工具,如CVS、SVN,如果希望有商业支持,Perforce只要$450,如果有钱,ClearCase是最好的选择。

做这 ...
echo_her 发表于 2009-10-2 20:34


不必这么苛刻。

我很欣赏小畜的作品。很不错。
回复 不支持

使用道具 举报

发表于 2009-10-3 18:16:29 | 显示全部楼层 来自 上海浦东新区
本帖最后由 echo_her 于 2009-10-3 18:24 编辑

风天小畜一直以来都是很热心的会员,我很佩服的。

我回帖的意思是:用合适的工具做合适的事情。不是想引起争论。

我用的Matlab 2006a,默认是支持VSS作为版本控制的,http://www.mathworks.com/matlabcentral/fileexchange/11596 提供了SVN在Matlab中使用的插件。

版本控制相关的目录是:toolbox\matlab\verctrl

评分

1

查看全部评分

回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-10-7 07:34 , Processed in 0.061904 second(s), 20 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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