(1) Matlab强制退出正在运行的程序
A: Ctrl + C
(2)如何让Matlab跑完程序后自动关机?
A: 在程序的末尾加上一条代码:
    system('shutdown -s')
   当然,记得在这条语句前加上保存结果的save,不然跑了很久的程序就白跑了。

(3) Matlab创建文件夹实例
A:  help exist查看下exist函数的用法
    示例: if exist('results')~=7
            mkdir('result')
          end
(4) Matlab中使用动态变量名
A: 编写程序时经常会遇到处理大量数据文件的情况,数据文件的命名比较相似,以一系列编号区分,如“a1.mat,a2.mat,... ,a100.mat " (假设其中的数据名称也为a1...a100) 。为了可以批量处理这些文件,可以采用如下的方法:
   for i=1:1:100
       s=strcat('a', int2Str(i));
       load(strcat(s,'.mat'));
       x=eval(s); %将数据a1赋值给x,便于后继统一处理
       % ...          %统一处理程序   
   end
为动态变量名赋值示例

for i=1:10
       eval(['A',num2str(i),'=rand(1,i)']);
   end
动态调用A1~A10
可以使用cell数组,因为A1-A10是长度不同的向量。
   for i=1:10
       B{i}=eval(['A',num2str(i)]);
   end
(5) 通过命令行运行matlab带参函数
在命令行输入
   matlab -r "functionanme firstpara secondpara"
注意事项:引号不能少;functionname不包含「.m」后缀
(6) 怎么判定一个文件或者文件夹是否存在
if isequal(exist(filename,'file'),2)   %2 means it's a file
if isequal(exist(pathname,'dir'),7)    %7 means it's a directory
(7)Matlab中用图片制作小电影
方法:使用VideoWriter()类
%% Movie Test.
%% Set up some function. 
% Sine between -2*pi and 2*pi.
x = (10*-pi:0.1:10*pi)'; % Note the transpose.
y = sin(x);
fid = figure;
hold on
% The final plot.
plot(x,y, '*'); 
%% Set up the movie.
writerObj = VideoWriter('out.avi'); % Name it.
writerObj.FrameRate = 60; % How many frames per second.
open(writerObj);  
for i=1:size(y)      
    % We just use pause but pretend you have some really complicated thing here...
    pause(0.1);
    figure(fid); % Makes sure you use your desired frame.
    plot(x(i),y(i),'or'); 
    %if mod(i,4)==0, % Uncomment to take 1 out of every 4 frames.
        frame = getframe(gcf); % 'gcf' can handle if you zoom in to take a movie.
        writeVideo(writerObj, frame);
    %end 
end
hold off
close(writerObj); % Saves the movie.
(8) Matlab计算L2欧氏距离
Matlab内置函数pdist2(p1,p2,'euclidean')
可以计算一个点和一组点的距离。示例:
% Define our points.
aPoint = [1,4]; % A single point with 2 components.
bunchOfPoints = [2,3; 1,4; 0,1]; % A bunch of other points. 
d = pdist2(aPoint,bunchOfPoints,'euclidean') 
(9) Matlab如何计算均方误差root-mean-square-error
函数rms
% The actual values that we want to predict.
Actual = [1 2 3 4]; 
% The values we actually predicted.
Predicted = [1 3 1 4]; 
% One way is to use the Root Mean Square function and pass in the "error" part.
rmse = rms(Predicted-Actual)
(10) Matlab计算平均绝对误差mean-absolute-error

函数mae
% The actual values.
Actual = [1 2 3 4]; 
% The values we predicted.
Predicted = [1 3 1 4];  
% You can just use the built in Mean Absolute Error function and pass in the "error" part.
builtInMAE = mae(Actual-Predicted)
(11) Matlab内置Random Forest算法

% Train the TreeBagger (Decision Forest).

nTrees = 20;
B = TreeBagger(nTrees,traindata,trainlabels, 'Method', 'classification');
predChar = B.predict(testdata);
predClass = str2double(predChar)

(12) 为图像中的部分pixel上色
源图像origImg; binary 图像maskImg其中0代表背景,1代表要上色的点。
%% Example on how to color select pixels in an image.
% The original COLOR image.
origImg = imread('ngc6543a.jpg');
 
% Make sure the values are within 0-255.
origImg = uint8(origImg);
 
% View the original image.
figure; fId = imagesc(origImg); axis image;
title('click and hold mouse to draw on the original image'); 
 
% The user draws on the image to select the pixels to highlight.
M = imfreehand();
 
% 0 = background pixels (do not change).
% 1 = foreground pixels (change these colors).
maskImg = M.createMask;
 
% View the black and white mask.
figure; imagesc(maskImg); colormap gray; axis image;
 
% Now let's color the mask green to make it more interesting. 
% To do this, we have to make three matrices, one for each color channel.
 
% Increase the color by half the max value so we can see some transparancy 
% in the original image.
amountIncrease = 255/2;
 
alphaImg(:,:,1) = zeros(size(maskImg)); % All zeros.
alphaImg(:,:,2) = round(maskImg*(amountIncrease)); % Round since we're dealing with integers.
alphaImg(:,:,3) = zeros(size(maskImg)); % All zeros. 
 
% Convert alphaImg to have the same range of values (0-255) as the origImg.
alphaImg = uint8(alphaImg);
 
% View alphaImg.
figure; imagesc(alphaImg); axis image;
 
% Combine the original images and the alpha values to highlight the select
% pixels.
blendImg = origImg + alphaImg;
 
% Show the blended images.
figure; imagesc(blendImg); axis image;
(13) print图片打印
print -fhandle -rresolution -dfileformat filename
例如print -f2 -r300 -djpeg myfigure命令将句柄为2的图像生成myfigure.jpg文件,分辨率为dpi300
如果要求矢量图,可以用-depsc 
如果要批量制图,参考如下循环流程
for kk = 1:10
%%%生成图像
% 如果需要调整图像大小
% 在存储前使用
% set(handle,'Position',[left, bottom, width, height])
% 例如 set(gca, 'Position',[80,100,800,600])
...
print('-r150','-depsc',['d:\image\myfigure',sprintf('%02d',kk)])
end
print 调整图像大小的命令
默认状态下打印大小为[0.25 2.5 8.0 6.0] in inches
调整命令三行连用
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperPosition', [2 1 4 2]);
units: inches,centimeters, normalized, points
(14) print命令保存eps文件与figure文件显示不同之解决方法
在print('-depsc','a.eps');一句前加上 "set(gcf,'paperpositionmode','auto');"  即可
(15) 使用matlab计算两幅图片的Jaccard距离
以下示例比较画线的相似度和距离
其中白色的pixel代表线;黑色的pixel代表背景
% JaccardTest.m
% Compute the Jaccard similarity coefficient (index) of two images. 
% A value of "1" = the line object (foreground).
% A value of "0" = the background.
 
% Alice draws a vertical line.
Alice = [0 1 0; 
         0 1 0; 
         0 1 0];
 
% RobotBob tries to draw a line.     
RobotBob = [0 0 0;
            0 1 1;
            0 0 1];
 
% Carol tries to draw a line.
Carol = [0 1 0; 
         0 1 0; 
         1 1 0;];
 
% Let's see their two drawings.
figure; 
subplot(1,3,1); imagesc(Alice); axis image; colormap gray; 
title('Alice''s nice line drawing');
 
subplot(1,3,2); imagesc(RobotBob); axis image; colormap gray; 
title('RobotBob tries to draw Alice''s line'); 
 
subplot(1,3,3); imagesc(Carol); axis image; colormap gray; 
title('Carol''s tries to draw Alice''s line'); 
 
% How similar are Alice's and Bob's drawing of a line? 
% An intuitive way to measure this is to compare each of the white "line" 
% pixels (a value of "1") to each other and see how many white pixels 
% overlap compared to the total number of white line pixels.
 
% We compute the intersection of the two lines using the "AND" operator "&".
intersectImg = Alice & RobotBob; 
figure; imagesc(intersectImg); axis image; colormap gray; title('intersection');
 
% We compute the union of the two lines using the "OR" operator "|".
unionImg = Alice | RobotBob;
figure; imagesc(unionImg); axis image; colormap gray; title('union');
 
% There is only one pixel that overlaps (intersects) 
numerator = sum(intersectImg(:));
 
% There are 5 pixels that are unioned.
denomenator = sum(unionImg(:));
 
% So intuitively we might expect that a similarity of 1/5 would 
% be a good indication. This is exactly what Jaccard's does.
 
jaccardIndex = numerator/denomenator
% jaccardIndex =
%     0.2000
 
% Jaccard distance shows how dis-similar the two line drawings are.
jaccardDistance = 1 - jaccardIndex
% jaccardDistance =
%     0.8000
 
%% How simililar are Alice and Carol's two line drawings?
 
% We can compute Jaccard's index in a single line,
jaccardIndex_ac = sum(Alice(:) & Carol(:)) / sum(Alice(:) | Carol(:))
%jaccardIndex_ac =
%     0.7500
%
% As expected, we can see that Alice's and Carol's drawing of a line is
% much MORE "similar" than Alice's and Bob's drawing (0.2).
 
% Let's check the Jaccard distance.
jaccardDistance_ac = 1 - jaccardIndex_ac
% jaccardDistance_ac =
%    0.2500
%
% As expected, we can see there is LESS "distance" between Alice's and
% Carol's drawing of a line than Alice's and Bob's drawing of a line (0.8).
通常我们不关心两幅图片的 Jaccard距离,我们关心的是图片中的形状之间的相似度。
---------------------
作者:xmjdh
来源:CSDN
原文:https://blog.csdn.net/lqhbupt/article/details/20292113
版权声明:本文为博主原创文章,转载请附上博文链接!

Matlab使用技巧的更多相关文章

  1. Matlab小技巧

    记录一些用Matlab的技巧. //imshow全屏 subplot(1,3,3); imshow(topSketMat); hold on; set(gcf, 'units', 'normalize ...

  2. matlab提速技巧(自matlab帮助文件)

    matlab提速技巧(自matlab帮助文件) 1.首先要学会用profiler.1.1. 打开profiler.To open the Profiler, select View -> Pro ...

  3. 小论文matlab作图技巧

    小论文matlab作图技巧 编辑->复制选项 编辑->图形属性 图中右击->字型 编辑->复制图片,即可. 效果: 宽:5.9高: 7.91

  4. MATLAB编程技巧

    [摘要] MATLAB是一种科学计算语言,和C.Fortran等高级语言相类似,能方便的实现程序控制.以下介绍一点matlab编程的技巧. 嵌套计算 程序执行的速度取决于调用的子程序的个数和算法实现. ...

  5. matlab 画图技巧

    基本画图工具:matlab 画图中线型及颜色设置 matlab中坐标轴设置技巧 **Matlab中的坐标轴设置技巧**    axisoff;      %去掉坐标轴  axistight;      ...

  6. matlab中小技巧

    关于matlab中可能遇到的小知识点 一.字符串的比较 不能使用“==”,需要使用函数strcmp() %matlab中字符串的比较 %字符串比较要用strcmp.相同则返回1,不相同则返回0. cl ...

  7. Matlab实用技巧

    1  Matlab Cell 编程模式 在一个长长的脚本m文件中,可能需要对其中的一段反复修改,查看执行效果,这时,cell模式就非常有用了.cell模式相当于将其中的代码拷贝到命令窗口中运行.两个% ...

  8. MATLAB常用方法技巧总结

    ===================================================================================================M ...

  9. Matlab小技巧之怎么复制汉字

    在我们复制Matlab到Word的过程中,经常会出现乱码的情况.这时候可以这么做. 1.复制Matlab代码. 2.新建一个txt文件,将代码粘贴到txt文件中. 3.复制txt文件中的代码到Word ...

随机推荐

  1. 搜藏一个php文件上传类

    <?php /** * 上传文件类 * @param _path : 服务器文件存放路径 * @param _allowType : 允许上传的文件类型和所对应的MIME * @param _f ...

  2. 【Dubbo&&Zookeeper】5、dubbo总结和学习资料汇总

    Dubbo学习资料 阿里巴巴分布式服务框架 Dubbo 团队成员梁飞专访 RPC介绍 什么是RPC? RPC(Remote Procedure Call)远程过程调用.见名知意 - 从远程主机调用一个 ...

  3. WINCE 下载地址(转)

    WinCE 6.0 安装包比较大,从微软下载时,它只提供一个下载工具,用它下载比较慢在网上查了些资料,把WinCE所需的安装包地址都收集起来了,安装包文件名都是有规律的,可以用迅雷新建批量任务来下载, ...

  4. 初学HTML-7

    表单中的一些标签 label标签:让文字和输入框进行绑定,即,点击文字,输入框可以开始输入(默认文字和输入框没有关系,不会聚焦) 格式:<form action=""> ...

  5. Python 简单的远程执行命令

    client端执行命令,server端返回命令结果 # server 端 import socket, subprocess sk = socket.socket() address=('127.0. ...

  6. 【读书笔记】iOS-深入解剖对等网络

    协议本身是一个运行在UDP之上的定制协议.我所以决定使用一个定制协议很简单.首先,当前这个任务看起来足够简单,因此与尝试改进一个现在协议相比,直接构建一个定制协议更为容易.其次,定制协议可以将开销减少 ...

  7. js 乘除法小数问题

    因为经常需要js来处理显示,就做下笔记 除法: function accDiv(arg1, arg2) { var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.to ...

  8. CentOS7.4 系统下 Tomcat 启动慢解决方法

    CentOS7.4 系统下 Tomcat 启动慢解决的方法   首先查看日志信息,查看因为什么而启动慢 在CentOS7启动Tomcat时,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是s ...

  9. Azure Ubuntu18.04安装lxde桌面记录,Windows远程连接Ubuntu18.04(Linux)

    执行如下命令: 尽量按以下顺序执行,否则可能会发生意向不到的问题(坑) 1.更新数据源 sudo apt-get update 2.更新安装包 sudo apt-get upgrade 3.安装lxd ...

  10. web测试之界面测试

    所谓界面测试就是指,布局是否合理.整体风格是否一致.各个控件的放置位置是否符合客户使用习惯,此外还要测试界面操作便捷性.导航简单易懂性,页面元素的可用性,界面中文字是否正确,命名是否统一,页面是否美观 ...