Find position of cell array within another cell array or table or in a structure

39 ビュー (過去 30 日間)
I have a number of nodes on a graph and a number of edges that connect them. Attached to each edge is a structure wear the first field is the EndNodes data from the edge table which defines the edges. I want to search the struct array to find the structure that matches the selected edge EndNodes but I can't figure out the syntax. The closest I've come is something like this where EdgeData is the struct array and data is the 2x1 cell containing the desired EndNodes:
selectedEdge=EdgeData(ismember(data,array2table({app.EdgeData.EndNodes}).Var1))
This fails because the inputs of ismember must be character arrays rather than cell arrays containing character arrays. Concatenating them could work but I would then simply be detecting if the two names of the edges are present and not whether they are contained together within one field. I've tried ==, ismember, strcmp, isequal and I've run out of things to try and ideas of documentation to comb through. It's possible that arrayfun might be the way forward but I don't really understand it honestly
PS yes I'm using app designer hence the referencing of EdgeData as a property
  2 件のコメント
Fangjun Jiang
Fangjun Jiang 約2時間 前
Providing an example of the data would be most helpful to understand the need. You can construct a simplifed data using code.
Timothy
Timothy 約1時間 前
Of course. As mentioned the data is all EndNode data which, in case you're not familiar with the required format of edges on graphs, are 1x2 cells (sorry not 2x1) where each cell in the array is the identifier which references a specific node - in this case each cell array contains two character arrays which is the name of a train junction.
For example each cell would look something like this:
data={{'Littleport'}{'West River'}}

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 約8時間 前
Are you using a graph or digraph object? Do you have the additional struct information stored as a separate variable or have you added that information as custom attributes in the Edges and/or Nodes properties of the graph/digraph object? I recommend the latter if you're using the object.
If you have that information as custom attributes of a graph or digraph object, to locate the data for a specific edge given the end nodes use the findedge function. Then use that edge index to index into the Edges property.
s = [1 1 2 2 3];
t = [2 4 3 4 4];
G = digraph(s,t);
G.Edges.Weight = [10 20 30 40 50]';
disp(G.Edges)
EndNodes Weight ________ ______ 1 2 10 1 4 20 2 3 30 2 4 40 3 4 50
whichEdge = findedge(G, 2, 3)
whichEdge = 3
theWeight = G.Edges{whichEdge, 'Weight'}
theWeight = 30
Note that if you've constructed the digraph using a possibly unordered set of source and target indices, and the additional edge information is stored in a separate variable, the order may be different. The EndNodes list order is not guaranteed to be the same as the order in which the edges were specified when the graph/digraph object was created.
s2 = [1 1 3 2 2];
t2 = [2 4 4 3 4];
w2 = [10 20 50 30 40];
D = digraph(s2, t2, w2);
D.Edges
ans = 5×2 table
EndNodes Weight ________ ______ 1 2 10 1 4 20 2 3 30 2 4 40 3 4 50
The third edge in the Edges property is not the one specified by the third elements in s2, t2, and w2. So if you were to findedge and use that to index into w2, you'd get the wrong answer. That's one reason I recommend storing both the edge information and the additional attributes inside the graph/digraph object.
E = findedge(D, 2, 3) % Correct edge located
E = 3
weight = w2(E) % but w2 is in a different order than Weight so wrongly returns 50
weight = 50
weight = D.Edges{E, "Weight"} % Correctly returns weight of 30
weight = 30
  2 件のコメント
Timothy
Timothy 約7時間 前
Thanks for that. I'm not storing the additional data in the table because some of the additional data is stored in separate tables which are kept within the structure. To be fair, the structures are currently added to the array at the same time as the edges are added to the table so this solution will still work, thanks. The graph is a digraph however, since it is simulating a rail system, each edge added automatically generates a reciprocal edge as the track goes both ways.
An ideal solution would be to search through the struct array by looking at the specified field but it seems that is not possible. I'm surprised I'm the first person to encounter this, though I suppose the struct and table storage solutions are parallel enough to make this requirement a bit niche.
Steven Lord
Steven Lord 約6時間 前
If you wanted to look for data in the custom attribute(s) and use that to select an edge or a set of edges, I'd use standard operations on one or more of the variables in the Edges table.
s = [1 1 2 2 3];
t = [2 4 3 4 4];
G = digraph(s,t);
G.Edges.Weight = [10 20 30 40 50]';
disp(G.Edges)
EndNodes Weight ________ ______ 1 2 10 1 4 20 2 3 30 2 4 40 3 4 50
whichEdgesHaveHighEnoughWeight = find(G.Edges.Weight > 25)
whichEdgesHaveHighEnoughWeight = 3×1
3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now I could use this to highlight the appropriate edges in the plot, as a potential example:
h = plot(G, EdgeLabel = G.Edges.Weight);
highlight(h, ...
G.Edges.EndNodes(whichEdgesHaveHighEnoughWeight, 1), ...
G.Edges.EndNodes(whichEdgesHaveHighEnoughWeight, 2), EdgeColor = 'r')

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2025 年 12 月 17 日 17:05
Something like this?
data={{'Littleport'},{'West River'}};
EndNodes={{'Bigport'},{'West River'};
{'Littleport'},{'West River'};
{'Littleport'},{'East River'}};
Index=ismember(string(EndNodes),string(data),'row')
Index = 3×1 logical array
0 1 0
  1 件のコメント
Timothy
Timothy 約9時間 前
編集済み: Timothy 約9時間 前
This gives me an error using string - no constructor 'string' with matching signature found.
Also, not that I can reproduce what you've done but in my experience transforming the data to a string and doing it this way prevents you from detecting the inputs in the correct order only?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by