define a function like python

Hello all,
I have a fuction in python but I want to write like this in Matlab.
def link_to_grasshopper(x):
# x is a numpy array
# part 1: from jupyter to GH
df = pd.DataFrame(remap(x))
df.to_csv('X.csv')
# wait pls
sleep(120.0)
# part 2: from GH back to jupyter
f = pd.read_csv('Fitness.csv')
return f.values[0][0]

5 件のコメント

Rik
Rik 2022 年 2 月 17 日
What is your question? If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
Walter Roberson
Walter Roberson 2022 年 2 月 17 日
Opps, sorry, it did not keep the spacing properly when I converted it to code :(
Walter Roberson
Walter Roberson 2022 年 2 月 17 日
Okay, could you explain what you mean by "write like this in Matlab" ?
Do you mean that you want to be able to have code that in the middle of a .m file, complete with having lines end with : and complete with using # as a comma?
Do you mean that you want to be able to create Python code in a .py file and refer to it from MATLAB without going through the python objects that would normallty be needed to get Python to execute it?
bahar vojdani
bahar vojdani 2022 年 2 月 18 日
Thanks for helping All in all I wanted to write a function gives some parameters,and creat csv file with parameters and after simulation in other software(sleep time) read file csv an return value of csv file
Image Analyst
Image Analyst 2022 年 2 月 18 日
So did you take what I gave below and try to continue with it? If not, why not? Let's see your latest version of it.

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

回答 (1 件)

Image Analyst
Image Analyst 2022 年 2 月 17 日

0 投票

Perhaps something like this:
function data = ReadGrasshopperFile(x, fileName1, fileName2)
% x is a numpy array
% part 1: from jupyter to GH
df = pd.DataFrame(remap(x))
% Write to CSV file
writematrix(df, fileName1);
% wait pls
sleep(120.0)
% part 2: from GH back to jupyter
data = readmatrix(fileName);
end
Or maybe at least it's a start, since I'm not really sure what all those things in your Python code are.

3 件のコメント

bahar vojdani
bahar vojdani 2022 年 2 月 19 日
Hello Dear,
I appreciate your help. In fact, I wanted to have a function that created a CSV file from my parameters in the first part, and this CSV file was imported to another simulation software. After the simulation, which takes around 120 seconds, read my results as a CSV file and return these results.
Thank you so much for your attention and participation.
bahar vojdani
bahar vojdani 2022 年 5 月 5 日
hello dear,
I have a python script, but I want to rewrite that in Matlab. Indeed, in python, I used the os module to help me interact with the operating system. 'os.path.getmtime(path)' Return the time of the last modification of the path.
def cost(x):
df1 = pd.DataFrame(x)
fileName = 'output.csv'
originalTime = os.path.getmtime(fileName)
df1.to_csv('inputs.csv')
while(True):
if(os.path.getmtime(fileName) > originalTime):
break
df2 = pd.read_csv('output.csv',memory_map=True)
d = []
for i,j in zip(df1.itertuples(),df2.iteritems()):
t = deepcopy(list(j[1]))
h = deepcopy(list(i))
h.pop(0)
results.loc[len(results.index)] = h + t
t[0] *= -1
d.append(t)
return d
Walter Roberson
Walter Roberson 2022 年 5 月 5 日
https://www.mathworks.com/matlabcentral/answers/9957-using-net-filesystemwatcher-to-listen-for-new-files#answer_13710

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

カテゴリ

ヘルプ センター および File ExchangeIntegration with Online Platforms についてさらに検索

質問済み:

2022 年 2 月 17 日

コメント済み:

2022 年 5 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by