本周文件交换精选

我们最好的用户提交

Catstruct

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

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

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

firststruct。X = PI secondstruct。X = exp(1) combinedstruct = [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]
Thirdstruct = struct with fields: x: 3.1416 fourthstruct = struct with fields: y: 2.7183错误使用horzcat
连接的结构数组中字段的名称不匹配。结构数组的连接要求这些数组具有相同的字段集。

如果你想要第一个,那你很幸运。这是可行的,因为结构有类似的字段名。如果没有,你就会得到第二个错误。这就是catstruct来拯救。

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

= catstruct(thirdstruct, fourthstruct)
Combinedstruct = struct with fields: x: 3.1416 y: 2.7183

我是在工作中发现catstruct的正则表达式解析自定义文本文件。我使用了不同的正则表达式,并一直以多个结构体结束。Catstruct使得把它们连接成一个非常容易。这里有一个典型的例子:

str =“你好,世界。15只敏捷的棕色狐狸跳过了3条2.3英尺高的懒狗。”%使用正则表达式提取片段。狐狸= regexp (str,(? <狐狸> \ d +) (? = \ w \ [s] *福克斯)”“名字”)狗= regexp(str,‘(? < =福克斯\ D *)(? <狗> \ D +) '“名字”) dogheight = regexp(str,‘(? < dogheight > [\ \ d] +)英国《金融时报》的“名字”) height = regexp(str,' \ d (? < heightunit > [a-zA-Z] +) \ s '“名字”
struct = struct with fields: Foxes: "15" dogs = struct with fields: dogs: "3" dogheight = struct with fields: dogheight: "2.3"

结合。

Info = catstruct(狐狸,狗,狗的高度)
Info = struct with fields: foxes: "15" dogs: "3" dogheight: "2.3" height tunit: "ft"

评论

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

发布与MATLAB®R2017a

|

评论

要留下评论,请点击在这里登录到您的MathWorks帐户或创建一个新帐户。