本篇博文记录一些笔者使用 Matlab 时,根据自己的需求编写的一些小函数。
Part.I 元胞相关
Chap.I 创建空 char 型元胞
matlab可以创建空元胞矩阵cell(a,b)
;只不过创建好之后里面存储的类型是空double,笔者想要创建一个空元胞矩阵且里面存放的数据类型是char,所以笔者编写了这样一个函数:
% get a null cell which is a*b dims
function data=kcell(a,b)
data=cell(a,b);
for i=1:a
for j=1:b
data(i,j)=cellstr(num2str(data{
i,j}));
end
end
end
Chap.II cell 矩阵转 char 矩阵
这个函数可以将存储char
类型的元胞矩阵,转换成char
矩阵。因为直接用cell2mat
会将所有的元胞内容合并到一个char
中,而不是所想要的char
矩阵。注意:每一个元胞中的char
需长度一致。
% cell mat 2 str mat
function data=cellm2strm(cemat)
n=length(cemat);
data=[];
for i=1:n
data=[data;cemat{
i}];
end
end
Chap.III cell 矩阵转 double 矩阵
如果cell中存储的是double
类型的数据,那么可以直接用cell2mat
函数将cell矩阵转换为double矩阵;若cell中存储的是char
类型的数据,就可以用下面的函数了。
% cell_char mat 2 double mat
function num=cell2num(data)
[a,b]=size(data);
num=zeros(a,b);
for i=1:a
for j=1:b
num(i,j)=str2double(data{
i,j});
end
end
end
Part.II 矩阵相关
Chap.I 矩阵插入元素
Matlab 在一个行向量/列向量某个位置处插入一个值
% insert num at ind in mat
function data=insert(mat,ind,num)
n=length(mat);
data(ind)=num;
data(1:ind-1)=mat(1:ind-1);
data(ind+1:n+1)=mat(ind:n);
end
Chap.II num 矩阵转 char 矩阵
通过调节'%02d'
来控制转换格式。这个函数特定应用于卫星prn转换,若想应用于其他方面,可以略作修改。
% this function can trans numList to charSatList
function satlist=num2sat_char(sys,num)
satlist=[];
for i=num
satlist=[satlist;sys,num2str(i,'%02d')];
end
end
Chap.III num 矩阵转 cell 矩阵
和上面的函数类似。返回的cell矩阵中存储的是char
类型的数据。
%this function can trans numList to cellSatList
function satlist=num2sat_cell(sys,num)
satlist={
};
for i=num
satlist=[satlist,cellstr([sys,num2str(i,'%02d')])];
end
end
Part.III 字符串相关
Chap.I 获取一个文件夹下所有文件的文件名的部分内容
最近搞事情想从一个目录中提取所有文件的文件名中前几个字符并转换为char
。原来发现我一直存在一个误区“""
和''
引起来的东西是相同的”。今天才发现是不同的,然后又学到了一写新的东西:比如元素取唯一,获取目录中所有文件的名字……
% get sitename from a dir
function site=getSite_dir(enudir)
dirOutput = dir(fullfile(enudir));% 此行+下面一行=获取目录所有文件名
plyName = {
dirOutput.name}; % get a cell mat
plyName = plyName(3:end); % rm . and ..
n=length(plyName);
sitelist="";
for i=1:n
fname=plyName{
i};
sitelist=strcat(sitelist," ",fname(1:4));
end
sitelist=unique(regexp(strtrim(sitelist), '\s+', 'split'));
%string2char
nsite=length(sitelist);
site=[];
for i=1:nsite
tmp=char(sitelist(i));
tmp=lower(tmp);
site=[site;tmp];
end
end
注:regexp
是以空格为分隔符将str
变为str array
。char
是用单引号引起来的字符串,string
是用双引号引起来的字符串,两者之间的转化用它们的名字即可;char
合并用[]
,string
合并用strcmp
。
Chap.II 比较两个 char 的大小
可以实现类似于C++
字符串比较的功能。
- str1<str2, return 1
- str1==str2, return 0
- str1<str2, return -1
% util compare two strings 1>1 1=0 1<-1
function p=mstrcmp(str1,str2)
k=min(length(str1),length(str2));
for n=1:k %Compare the top k
if(str1(n)>str2(n))
p=1;break;
elseif(str1(n)==str2(n))
p=0;
else p=-1;break;
end
end
if(p==0)
if(length(str1)>length(str2)) %The first k bits are equal, but str1 is longer
p=1;
elseif(length(str1)==length(str2))
p=0;
else p=-1;
end
end
end
Chap.III 查找索引
查找一个 prn
在一个 char
矩阵中的索引
sat是个char
类型的,satlist是个char
矩阵,返回索引值
%this function can find the sat index from a charSatList
function prn=find_sat_char(satlist,sat)
n=size(satlist,1);
prn=0;
for i=1:n
if mstrcmp(satlist(i,:),sat)==0
prn=i;
break;
end
end
end
查找一个 prn 在一个 cell 矩阵中的索引
sat是个char
类型的,satlist是个cell
矩阵,返回索引值
%this function can find the sat index from a cellSatList
function prn=find_sat_cell(satlist,sat)
n=length(satlist);
prn=0;
for i=1:n
if mstrcmp(satlist{
i},sat)==0
prn=i;
break;
end
end
end
Part.IV 结构体相关
Chap.I 读取结构体
比如我现在想读取这样一个文件到结构体中。文件内容如下:
name sex age hobby
aa man 4 8
ab wom 5 9
bb wom 6 10
cc man 7 11
实现函数如下:
% get struct from clkdif file
function stu=read_dif(file)
fid=fopen(file,'r');
str = fgetl(fid);
S = regexp(str, '\s+', 'split'); %field
n=length(S);
j=1;
while ~feof(fid)
str = fgetl(fid);
str=strtrim(str); %rm the blankSpace on the beg and end
if str(1)=='-'
continue;
end
if str(1:3)=='EOF'
break;
end
tmp = regexp(str, '\s+', 'split');
for i=1:n
eval(['stu(j).',S{
i},'=tmp{i};']);
end
j=j+1;
end
fclose(fid);
end
调用示例:
clc;clear;
file='C:\Users\OHanlon\Desktop\a.txt';
tic;
stu=read_dif(file);
toc;
Chap.II 取结构体中某一字段的所有值
可以和上面的函数配合使用
% get the field value from sta
function data=get_fdata(sta,field)
data=[];
af=fieldnames(sta);
n=length(af);
for i=1:n
if mstrcmp(af{
i},field)==0
break;
end
end
if (mstrcmp(af{
i},field)~=0)
disp('----error----');
disp(['The filed ''',field,''' is not in sta!']);
disp('------end----');
return;
end
m=length(sta);
for i=1:m
data=eval(['[data;sta(i).',field,'];']);
end
end
Part.V 数据处理
Chap.I 计算加权平均值
此函数可以计算一组数据的加权平均值,输入的数据有两列,第一列是待计算加权平均值的一组数据,第二列是其对应的权;权会在函数中归一化,所以不用在意其绝对大小;函数会返回其加权平均值。
% Util: compute ave with weight
function ave=ave_weight(data)
std=data(:,1);
weight=data(:,2);
weight=weight./sum(weight);
ave=sum(std.*weight);
end