文件交换的选择

我们最好的用户提交

Catstruct

肖恩本周的选择catstruct通过乔斯(10584)

你曾经有两个结构和想结合成一个?

上述问题是模棱两可的。你的意思是什么?

firststruct。x =πsecondstruct。x = exp (1) combinedstruct = [firststruct secondstruct]
firststruct =结构体字段:x: 3.1416 secondstruct =结构体字段:x: 2.7183 combinedstruct = 1×2结构体数组字段:x

还是?

thirdstruct。x =πfourthstruct。y = exp (1) combinedstruct = [thirdstruct fourthstruct]
thirdstruct =结构体字段:x: 3.1416 fourthstruct =结构体字段:y: 2.7183错误使用horzcat
要连接的字段结构数组的名称不匹配。串联的结构数组要求这些数组具有相同的字段。

如果你想要第一个你在运气。这工作,因为结构和字段名。如果他们不这样做,你会在第二个错误。这就是catstruct来救援。

没有cat-struct。这一个!

combinedstruct = catstruct (thirdstruct fourthstruct)
y combinedstruct =结构体字段:x: 3.1416: 2.7183

我发现catstruct同时使用正则表达式解析一个自定义的文本文件。我使用单独的正则表达式和保持结束了多个结构体。Catstruct连接成一个很简单。这是一个代表性的例子:

str =“Hello World。15只敏捷的棕色狐狸跳过3 2.3英尺高的懒狗”;使用正则表达式%提取部分。狐狸= regexp (str,(? <狐狸> \ d +) (? = \ w \ [s] *福克斯)”,“名字”)狗= regexp (str,‘(? < =福克斯\ D *)(? <狗> \ D +) ',“名字”)dogheight = regexp (str,(? < dogheight > [\ \ d]。+)英国《金融时报》的,“名字”)heightunit = regexp (str,' \ d (? < heightunit > [a-zA-Z] +) \ s ',“名字”)
狐狸=结构体字段:狐狸:“15”狗=结构体字段:狗:“3”dogheight =结构体字段:dogheight:“2.3”heightunit =结构体字段:heightunit:“英尺”

结合。

信息= catstruct(狐狸、狗、dogheight heightunit)
信息=结构体字段:狐狸:“15”狗:“3”dogheight:“2.3”heightunit:“英尺”

评论

试一试,让我们知道你的想法在这里或者离开评论乔斯。

发表与MATLAB®R2017a

|

评论

留下你的评论,请点击在这里MathWorks账户登录或创建一个新的。