档案交换一周

我们最好的用户提交

catstruct

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

您是否曾经有两个结构并想将它们合并为一个?

以上问题是模棱两可的。你的意思是?

firstStruct.x = pi secondstruct.x = exp(1)组合= [firststruct secondstruct]
firststruct = struct with fields: x: 3.1416 secondstruct = struct with fields: x: 2.7183 combinedstruct = 1×2 struct array with fields: x

或者?

thirdstruct.x = pi fourthstruct.y = exp(1) combinedstruct = [thirdstruct fourthstruct]
tixtStruct =带有字段的结构:x:3.1416 firtthstruct = with fields:y:2.7183使用HORZCAT错误
Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.

Well if you wanted the first one you’re in luck. That worked because the structures have like fieldnames. If they don’t, you get the error in the second one. This is wherecatstruct来营救。

不,不是那个猫的结构。这个!

组合= catStruct(第三结构,第四系列)
组合=结构与字段:x:3.1416 y:2.7183

我在工作时发现了猫常用表达to parse a custom text file. I used separate regular expressions and kept ending up with multiple structs. Catstruct made joining them into one really easy. Here’s a representative example:

str =“你好世界。15个快速棕狐跳超过3 2.3英尺高的懒狗”;%使用正则表达式提取零件。foxes = regexp(str,'(?\d+)(?=[\w\s]*fox)',,,,“名字”)dogs = regexp(str,'(?<= fox \ d*)(? \ d+)',,,,“名字”)dogheight = regexp(str,'(? [\。\ d]+)ft',,,,“名字”)heightunit = regexp(str,'\ d(? [a-za-z]+)\ s',,,,“名字”
狐狸=带有场的结构:狐狸:“ 15”狗=带有田野的结构:狗:“ 3” dogheight = with fields with fields:dogheight:“ 2.3” heightunit = with fields with fields:heighterunit:heightunit:“ ft”

Combine.

info = catstruct(foxes, dogs, dogheight, heightunit)
info =带有字段的结构:狐狸:“ 15”狗:“ 3” dogheight:“ 2.3” heightunit:“ ft”

Comments

Give it a try and let us know what you thinkhere或留下一个评论对于乔斯。

与Matlab®R2017A一起出版

|
  • 打印
  • 发送电子邮件

Comments

要发表评论,请单击here登录您的数学帐户或创建一个新帐户。