First, do some example data:
n = 10; ID = num2cell(randperm(n)'); Coordinate = mat2cell(randn(n, 3), ones(n,1)); Misc = num2cell(randn(n,1)); Conn = arrayfun(@randperm, randperm(n), 'UniformOutput', 0)'; Point = struct('ID', ID, 'Coordinate', Coordinate, 'Misc', Misc, 'Conn', Conn);
Now check it out:
>> Point Point = 10x1 struct array with fields: ID Coordinate Misc Conn >> Point(1) ans = ID: 7 Coordinate: [-0.0571 -1.1645 2.4124] Misc: 0.0524 Conn: [3 2 1]
Now use arrayfun()
to scroll arrayfun()
elements of the array of the Point
structure. We define a generic function x
to work with each Point
element, formatting the string as you described:
Point_cell = arrayfun(@(x) [num2cell([x.ID x.Coordinate x.Misc]) num2str(x.Conn)], Point, 'UniformOutput', 0); Point_cell = vertcat(Point_cell{:})
Now check the output:
ans = [ 7] [-0.0571] [-1.1645] [ 2.4124] [ 0.0524] '3 2 1' [ 5] [ 0.2918] [ 0.4948] [ 0.7402] [-1.9539] '1 2' [ 3] [-0.6146] [-1.2158] [ 0.3097] [ 0.5654] '3 4 1 2' [10] [-0.0136] [ 1.5908] [-0.5420] [ 0.0778] [1x25 char] [ 2] [ 0.4121] [ 0.5265] [ 0.1223] [ 0.0807] [1x22 char] [ 1] [-0.9371] [ 0.2648] [ 0.9623] [ 0.7947] '1 2 5 4 3' [ 4] [ 0.8352] [-0.3936] [-0.2540] [ 1.0437] '6 2 3 7 4 1 5' [ 8] [ 1.0945] [-2.1763] [ 1.8918] [ 0.8022] '1' [ 6] [ 0.3212] [-1.1957] [-1.2203] [-0.4688] [1x37 char] [ 9] [ 0.0151] [ 0.3653] [-0.3762] [-0.0466] '3 5 4 2 6 1'
I canβt say from your question, but if you want all numeric fields to be an array inside one cell, this is a simple setup. Good luck
source share