multidimensional array - Plotting a nested cell as a predefined structure: MATLAB -


i have series of nested cells represent tree structures. example of 1 of these cells corresponding structure image below:

i need know possible methods draw corresponding structure in matlab. far, have found this method here might best solution available, still struggling how implement it. thanks.

just doesn't remain unanswered...

mycellarray = {0,0,{1,0,0,{1,0},0,0},{1,0,{1,0},0},{1,1},0,0}; [mytreearray,mytreeevals] = gettreearray(mycellarray); mytreeplot(mytreearray,mytreeevals) 

for fourth example output, can modify suit needs: enter image description here

two functions, adapted answers, shown below.

first based on related answer @wolfie:

function [treearray, nodevals]  = gettreearray(cellarray)     % initialise array construction node 0     [nodes, ~, nodevals] = treebuilder(cellarray, 1,0);      nodevals = decellify(nodevals);     treearray = nodes-1;     % recursive tree building function, pass cell array , root node     function [out, node, nodevals] = treebuilder(cellarray, rnode,skipfirst)         % set variables populated whilst looping         out = []; nodevals = [];         % start node off @ root node         node = rnode;         % loop on cell array elements, either recurse or add node         num2loop = 1:numel(cellarray);         % added, input function everywhere used         if skipfirst             num2loop = 2:numel(cellarray);         end         %end added         ii = num2loop             tb = []; node = node + 1;             if iscell(cellarray{ii})                 [tb, node] = treebuilder(cellarray{ii}, node,1);             end             out = [out, rnode, tb];         end         nodevals = [nodevals,cellarray];     end      function data = decellify(data)         try             data = cellfun(@decellify,data,'un',0);             if any(cellfun(@iscell,data))                 data = [data{:}];             end         catch             % non-cell node, return node data as-is         end     end end 

and based on so answer treeplots:

function mytreeplot(treearray,nodevals) % @ first need get x , y coordinates of every node in original tree plot , find leaves in [x,y] = treelayout(treearray); leaves = find( y == min(y) ); %added if nargin < 2     leaveparents = find( y ~= min(y) );     leavechilds = leaves; else     leaveparents = find([nodevals{:}]==1);     leavechilds = find([nodevals{:}]~=1); end %end added num_layers = 1/min(y)-1; chains = zeros(num_layers, length(leaves)); % next, reconstruct every chain in tree plot , store in matrix (by doing so, can later change y position of nodes) l=1:length(leaves)     index = leaves(l);     chain = [];     chain(1) = index;     parent_index = treearray(index);     j = 2;     while (parent_index ~= 0)         chain(j) = parent_index;         parent_index = treearray(parent_index);         j = j+1;     end     chains(:,l) = padarray(flip(chain), [0, num_layers-length(chain)], 'post'); end % compute new y-coordinates determined row index in matrix , dependent on number of layers in tree: y_new = zeros(size(y)); i=1:length(treearray)     [r,c] = find(chains==i, 1);     y_new(i) = max(y) - (r-1)*1/(num_layers+1); end % can plot re-positioned nodes , add connecting lines: figure;plot(x(leaveparents), y_new(leaveparents), '.r','markersize',16); hold on plot(x(leavechilds), y_new(leavechilds), '.k','markersize',16); c=1:size(chains, 2)     line_x = x(chains(chains(:,c)>0, c));     line_y = y_new(chains(chains(:,c)>0, c));     line(line_x, line_y); end axis([0 1 0 1]); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -