Matlab使用中遇到的问题

本文最后更新于:2021年1月8日 晚上

一、Matlab中如何保存无白边图片

img=imread('C:\Users\GWL\Desktop\arun.jpg');
imshow(img,'Border','tight');
frame=getframe(gcf);

% method 1
imwrite(frame.cdata(:,:,1),'test.png');

% method 2
imm = frame2im(frame);
imwrite(imm,'test2.png');

test

test2

二、Matlab中double、char和cell间的互相转换

任务需求:读取txt文档,并将其数据存入cell中

  • 格式转换

cell2mat()  %将cell转换为mat的char型 
str2num()   %将mat从char转换为double型 
cellstr()   %将char转cell 
num2str()   %将double转char 
num2cell()  %将double直接转cel
  • 数字或字符前面补0

%原先为1234,转换为001234
b=num2str(a,'%06d');
  • 读取txt文件

A=load('E:\VOC2007\ImageSets\Main\test.txt');
%A=importdata('E:\VOC2007\ImageSets\Main\test.txt');

示例1

files = dir( ['\*.txt']);
name = files.name;
A = importdata(name);
info = A.textdata;
B(1).objects(1).bbox(:,1)=str2num(cell2mat(info(j,4)));         B(1).objects(1).bbox(:,2)=str2num(cell2mat(info(j,5)));

示例2

1×4093的double → 4093×1的cell

test2=num2str(test,'%06d')
test3=cellstr(test2)
save('test.mat','test3')

参考链接

【1】一个简单的字符串前补0的方法

【2】matlab中double、char和cell的互转

三、Matlab 如何调用python脚本

我们可以通过Matlab的system指令,实现对python脚本的调用。

system指令是向Windows系统cmd界面发送指令,并返回结果

  • status在指令正确运行时,返回0;运行错误时,返回非0值
  • cmdout返回cmd界面的输出值

因此,实现方法为:

[status,cmdout]=system(python D:\callpy.py)

注:

(1)上述成功运行的前提是已经为python配置了环境变量

(2)命令行中直接调用python脚本