关于Matlab下的cell元胞与struct结构体
来源:互联网 发布:java面向对象测试题 编辑:程序博客网 时间:2024/06/06 18:00
结构体数据类型可以把不同数据类型的变量放到同一个变量名下,通过不同“域”的概念对结构体中的不同数据进行赋值‘操作。结构体中的数据存储在相应的“域”中。
结构体的创建主要通过命令行直接赋值法和函数法。
1. 命令行直接赋值法
>>s.str='ABC';
>>s.num='[1:7];
>>s.mat=magic(3);
2 函数法
s=struct('str','ABC','num',[1:7],'mat',magic(3))
元胞数组是由可以包括任何数据类型的元胞组成的数组。通过元胞数组的使用,可以在同一个变量中存储不同数据类型的数据。
元胞数组的创建方法主要有直接赋值法和函数法。
1. 直接赋值法
>>a{1,1}=7;
>>a{1,2}='ABC';
>>a{2,1}=magic(3);
>>a{2,2}=logical(ones(3));
2. 函数法
>>a=cell(m,n);
>>a={7;'ABC';magic(3);logical(ones(3))}
元胞cell与结构体struct相互赋值之间为了类型一致,有时需要cell2struct或struct2cell,或对元胞的元胞或结构体的结构体需要用cellfun或structfun。但只要在赋值时正确使用()和{}便不需要使用这些函数进行转换。如下所示:
metric_dist={};for i=1:length(dist.label) metric_dist{i}.label=dist.label(i); metric_dist{i}.ind=dist.ind(i); metric_dist{i}.name=dist.name(i); endp_distance=metric_dist;
得到的p_distance如下:
现把代码只作()或{}修改如下:
metric_dist={};for i=1:length(dist.label) metric_dist(i).label=dist.label(i); metric_dist(i).ind=dist.ind(i); metric_dist(i).name=dist.name(i); endp_distance=metric_dist;
此时,p_distance变成了结构体,如下所示:
现把代码再修改为如下:
metric_dist={};for i=1:length(dist.label) metric_dist{i}.label=dist.label{i}; metric_dist{i}.ind=dist.ind{i}; metric_dist{i}.name=dist.name{i}; endp_distance=metric_dist;此时,p_distance也是结构体:
这里看上去和第一段代码结果一样,但具体到结构体内存储的内容,还是有差别的,这只是元胞cell对()取元胞单元,对{}取元胞内容的差别。
阅读全文
0 0
- 关于Matlab下的cell元胞与struct结构体
- MATLAB基本操作(四):结构体struct&元胞数组cell
- MATLAB基本操作(四):结构体struct&元胞数组cell
- 关于Matlab元胞结构体的赋值问题
- matlab的元胞和结构体
- linux下与实践相关的结构体 struct timeb,struct timeval ,struct timezone,struct timespec,struct tm 等
- linux下与时间相关的结构体 struct timeb,struct timeval ,struct timezone,struct timespec,struct tm 等
- [Matlab]结构体(Struct)
- matlab 结构体struct
- matlab struct 结构体
- matlab-结构体struct
- [Matlab]结构体(Struct)
- matlab:创建cell结构体
- MATLAB元胞数组(cell) I
- MATLAB元胞数组(cell) II
- matlab—元胞数组cell
- 关于结构体struct的讨论
- 关于struct msghdr结构体的介绍
- 2017个人知识内容
- 第九周 项目3
- spark redis 中碰到java.net.SocketTimeoutException: Read timed out问题的解决
- GYM Amman 17 G. Super Subarray 暴力,最小公倍数.
- python输出不自动换行格式
- 关于Matlab下的cell元胞与struct结构体
- 回溯法(4)
- Docker之私有仓库搭建-yellowcong
- 进程与线程的区别与联系
- 轮播图
- PCB制作中的过孔
- problems encountered during text search
- VS2017写代码时几个常用的快捷键
- 回溯法(5)