今天整理一下最近MATLAB的一些小小心得,not big deal but little tricks.
1.关于power function 和 nthroot
MATLAB中输入
1 |
(-8)^(1/3) |
时,往往会得到
1 2 |
ans = 1.0000 + 1.7321i |
因为我想要的是实数解-2,而不是复数.
这让我感到十分有意思,于是doc power后,发现文件中说
For negative base A and noninteger B, if abs(B) is less than 1, the power function returns the complex roots of A.
Use the nthroot function to obtain the real roots.
于是用输入
1 |
nthroot(-8,3) |
会得到
1 2 |
ans = -2 |
这个时候,我很好奇MATLAB中power函数的输出原则是什么。于是自己动手探究了一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
theta = -8; B1=1; filename = 'imag_roots.gif';%output file name B2=(3:13);%power nImages=length(B2); figure; for idx=1:nImages; % get the roots p=zeros(B2(idx)+1,1); p(1)=(theta); p(B2(idx)+1)=-1; t= roots(p); realt = real(t); imagt = imag(t); t2 = (theta)^(-B1/B2(idx)); Xlim1 = -1.6;Xlim2 = 1.6; Ylim1 = -1.6;Ylim2 = 1.6; plot(realt,imagt,'*b',real(t2),imag(t2),'or','MarkerSize',15); line([0,0],[Ylim1,Ylim2],'color','k','linewidth',2);%X=0 axis line([Xlim1,Xlim2],[0,0],'color','k','linewidth',2);%Y=0 axis line([0,real(t2)],[0,imag(t2)],'color','g','linewidth',2);% legend('roots','output of power'); xlim([Xlim1,Xlim2]);ylim([Ylim1,Ylim2]) xlabel('real');ylabel('imag'); title(strcat('roots of (',num2str(theta), ')^{-',num2str(B1),'/',num2str(B2(idx)),'}')); set(gca,'visible','on'); set(gcf,'Position',get(0,'Screensize'));%Maximize figure drawnow frame = getframe(1); im = frame2im(frame); [A,map]=rgb2ind(im,256); if idx==1 imwrite(A,map,filename,'gif','Loopcount',inf,'DelayTime',1); else imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1); end end close all |
得到以下gif
ActiveX在广义上是指微软公司的整个COM架构,但是现在通常用来称呼基于标准COM接口来实现对象链接与嵌入(OLE)的ActiveX控件。
而我们可以通过调用ActiveX来做许多事情,比如处理Excel中的数据。
1 2 |
Excel = actxserver('excel.application');% Create COM server Excel.Visible = true;% Make the Excel frame window visible. |
1 2 |
WB = invoke(Excel.Workbook,'open',filename); SH = WB.Worksheets.Item(<sheet number>); |
基本上Excel里能够达成的操作都可以用ActiveX+MATLAB完成,十分强大。
4.让保存的图片更加清晰
很多时候我们会发现保存的图片都缩紧到一起了,十分不好看。只需要在code里面加上一句命令就可以让figure最大化后再保存,效果很好。
1 |
set(gcf,'Position',get(0,'Screensize')) |
5.cellfun
我们经常发现很多对矩阵有用的函数,用在class cell的对象上就不行了。然而cell类本身十分强大的,尤其是储存字符类的时候,我们还是经常会碰到。这个时候强大的cellfun就派上用场了。cellfun的句法是
1 |
[A1,...,Am] = cellfun(func,C1,...,Cn,Name,Value) |
其中func是个function handle。这样我们其实自己可以写自己的cellfunc.
示例如下:
a是一个cell,我们想要算出a的mean。
1 2 3 |
a{1} = [1 2;3 4]; a{2} = [1 2 3;4 5 6; 7 8 9]; mean_a = mean(cellfun(@(x) mean(x(:)), a)); |
亲测很有用。
6.文件夹及文件处理
当我们需要处理大量文件或文件夹时,例如修改名称,这时MATLAB也可以派上用场。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
files = dir(path0);% Get all files & folders in current folders dirflags = [files.isdir]; subfolders = files(dirflags); for k = 3:length(subfolders) % attention starts from 3! sprintf('Now we are in the folder #%d = %s\n', k, subfolders(k).name) path1 = strcat(path0,subfolders(k).name,'\'); cd(path1); mkdir('NewName'); path2 = strcat(path1,'NewName\'); % Get all pdf files in the current folder files = dir(strcat(path1,'*.xlsx')); for ID = 1:length(files) [~,oldName] = fileparts(files(ID).name); % Rename newName = <your new name>; movefile(strcat(path1,files(ID).name),strcat(path2,sprintf('%s.<format of file>',newName))); movefile(strcat(path2,sprintf('%s.xlsx',newName)),strcat(path1,sprintf('%s.xlsx',newName))) end rmdir('NewName'); end cd(path0) |
今天就分享这些,希望对大家有用。欢迎各位踊跃留言!
【欢迎订阅并关注微信公众号“饱蠹阁BaoDuGe”】
You must be logged in to post a comment.
Carren
2017年3月12日
感觉很实用!
baoduge
2017年3月12日
嗯嗯,little tricks of MATLAB
Serene
2017年3月12日
gif做的很棒,结果很直观。这几个小技巧确实很赞,很用心。以后要是可以分类整理发布感觉会更治愈强迫症,遇到问题也便于查询到相关文档。
baoduge
2017年3月12日
多谢留言!我是想等内容多一些之后再分栏目。我都有注明分类的。
Tingting
2017年3月17日
点赞!继续加油呀!