Effacer les filtres
Effacer les filtres

Calling python in MATLAB

3 vues (au cours des 30 derniers jours)
Anthony Sirico
Anthony Sirico le 9 Nov 2020
In the below code, I have a Depth First Search with the pseudocode and the python code. My first question is how can I call this program in MATLAB?
I can use the following:
system('python DFS.py')
But I want to call it like:
And store the data in MATLAB, but when I execute the code it says Unable to resolve the name py.DFS.
My second question is, how can I change the code in python to where it takes a MATLAB adjacency list and runs that in the code, instead of putting the adjacency list directly in the python code?
#DFS(G, u)
#u.visited = true
#for each v within G.adj[u]
#if v.visited == false
#init() {
#For each u within G
#u.visited == false
#For each u within G
# DFS algorithm in Python
# DFS algorithm
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
for next in graph[start] - visited:
dfs(graph, next, visited)
return visited
graph = {'1': set(['2', '4', '5']),
'2': set(['1', '4', '3']),
'3': set(['2', '4', '6']),
'4': set(['1', '2', '3', '5', '6', '7']),
'5': set(['1', '4', '7', '8']),
'6': set(['3', '4', '7', '10']),
'7': set(['4', '5', '6', '8', '10']),
'8': set(['5', '7', '9']),
'9': set(['8', '10']),
'10': set(['6', '7', '9'])}
dfs(graph, '1')
n = len(graph)
s = pow(n, n-2) #number of spanning tree graphs
print("This is the number of spanning trees: ", s)

Réponses (1)

Abhisek Pradhan
Abhisek Pradhan le 12 Nov 2020
You could try using the Python interface introduced in MATLAB R2014b. You would need Python on your machine.
A few resources which might help:
And for using MATLAB workspace variables in Python refer the an example mentioned link below.


En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by