当前位置: 首页 > news >正文

Matlab 实用小函数汇总

文章目录

  • Part.I 元胞相关
    • Chap.I 创建空 char 型元胞
  • Part.II 矩阵相关
    • Chap.I 矩阵插入元素
  • Part.III 字符串相关
    • Chap.I 获取一个文件夹下所有文件的文件名的部分内容
  • Part.IV 结构体相关
    • Chap.I 读取结构体
    • Chap.II 取结构体中某一字段的所有值


本篇博文记录一些笔者使用 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:afor j=1:bdata(i,j)=cellstr(num2str(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

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:nfname=plyName{i};sitelist=strcat(sitelist," ",fname(1:4));
end
sitelist=unique(regexp(strtrim(sitelist), '\s+', 'split'));
%string2char
nsite=length(sitelist);
site=[];
for i=1:nsitetmp=char(sitelist(i));tmp=lower(tmp);site=[site;tmp];
end
end

注:regexp是以空格为分隔符将str变为str arraychar是用单引号引起来的字符串,string是用双引号引起来的字符串,两者之间的转化用它们的名字即可;char合并用[]string合并用strcmp

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 endif str(1)=='-'continue;endif str(1:3)=='EOF'break;endtmp = regexp(str, '\s+', 'split');for i=1:neval(['stu(j).',S{i},'=tmp{i};']);endj=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:nif mstrcmp(af{i},field)==0break;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:mdata=eval(['[data;sta(i).',field,'];']);
end
end
http://www.lryc.cn/news/22807.html

相关文章:

  • Echarts 仪表盘倾斜一定角度显示,非中间对称
  • Vue中如何利用websocket实现实时通讯
  • ​力扣解法汇总1144. 递减元素使数组呈锯齿状
  • Spring彻头彻尾的讲解,按照Spring框架启动流程,逐步剖析问题,不再是大杂烩!
  • [2]MyBatis+Spring+SpringMVC+SSM整合一套通关
  • Javascript的API基本内容(三)
  • 【Python入门第十九天】Python 函数
  • web前端性能优化
  • Telnet 基础实验2: SSH 实验
  • Panda Farm:首个部署在 Arbitrum 上的轻量化 GameFi 游戏
  • Redis实现分布式锁
  • 刷题小抄1-2数之和
  • axicom的测试文档
  • 基于vue3异步组件、动态组件、vite批量导入实现路由权限动态管理(非addRoute方案)
  • 带中转hub的卡车无人机车辆路径问题
  • 前端食堂技术周刊第 72 期:Signals 是前端框架的未来、Chrome Headless、ts-reset、magic-regexp、Bun 新文档
  • mysql中用逗号隔开的字段作查询用(find_in_set的使用)
  • Day902.Memory存储引擎 -MySQL实战
  • Linux(Centos)安装RabbitMQ+延时插件+开机自启动
  • 最近是遇到了CKPT(BLOCKED)
  • RabbitMQ死信队列
  • Word控件Spire.Doc 【书签】教程(1):在C#/VB.NET:在 Word 中插入书签
  • 微服务框架-学习笔记
  • 实验心理学笔记01:引论
  • 预备3-如何学习编程
  • 操作系统权限提升(十七)之绕过UAC提权-Windows令牌概述和令牌窃取攻击
  • 【时间之外】系统管人,能行?(冷眼旁观连载之二)
  • 【数据结构必会基础】关于树,你所必须知道的亿些概念
  • 设计模式的应用(已在大型项目中使用)
  • Git的相关用法