How to pass the variables from matlab workspace to python function, and run this python function in matlab

조회 수: 19 (최근 30일)
I am trying to pass the variables from matlab workspace to python function which scripting as:
def f():
global x,y
return x+y
and then I calling this python function in matlab command window by typing ' py.f.f' , however, the error occurred: Python Error: NameError: global name 'x' is not defined .
It would be much appreciated that if someone could help me to solve this, pass the variable from matlab to python function, and call the python function in matlab.

채택된 답변

Robert Snoeberger
Robert Snoeberger 2016년 4월 1일
>> mod = py.importlib.import_module('f');
>> py.setattr(mod, 'x', 1);
>> py.setattr(mod, 'y', 2);
>> py.f.f
ans =
3
>>
  댓글 수: 1
Pengfei Li
Pengfei Li 2018년 9월 11일
hello,
I want to pass the matlab data to python to create a csc sparse matrix using python language in matlab. now I met a problem, can you help me? I know you are an expert in this way, because I look many of you answers. Thank you very much. my code in matlab as follow, I met the data type problem.
iK=[0 2 2 0 1 2];
jK=[0 0 1 2 2 2];
sK=[1 2 3 4 5 6];
data=py.numpy.array(sK);
ir=py.numpy.array(iK);
jr=py.numpy.array(jK);
A = py.scipy.sparse.csc_matrix(data,ir,jr);
when I came to create the csc_matrix, then python would tell me the data type is not right. could you help me correct it? thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Stalin Samuel
Stalin Samuel 2016년 4월 1일

Amir tb
Amir tb 2020년 6월 19일
Dear firends, I'm new in Matlab coding. This is a code my teacher sent to me. It's about reading 5 .wav sounds with different lengths. The purpose is dividing each sound samples by 2000 sample for each and then figure it and process each figure seperately. For example for a sound with a length of 70000, it devides the sound into 35, 2000 samples which give us 35 seperate figures for each 200 sample.
My question is would you please explain me the code line by line?
And other question is how can I change the code such that having saved figure of each sample seperately in a new directory?
clc
clear all
close all
normrate=255;
numsamp=2000;
signal={};
image={};
zero=zeros(normrate,numsamp);
files = dir(fullfile('C:\Users\Amir\Desktop\Articles\PCG classification\matlab code\mamadreza\*.wav'));
for i=1:length(files)
X=audioread(files(i).name);
for k=1:length(X)/numsamp
Aug(:,k)=X((k-1)*numsamp+1:k*numsamp);
normalize=(Aug(:,k)-min(Aug(:,k)))*normrate/(max(Aug(:,k))-min(Aug(:,k)));
imshow(uint8(zero))
hold on
plot(normalize, 'w', 'LineWidth', 2)
f=getframe(gcf);
new=im2bw(f.cdata);
[t,r]=find(new==0);
final(:,:,k)=new(min(t):max(t), min(r):max(r));
close
end
signal{i}=Aug;
image{i}=final;
clear Aug
end

Pierre Harouimi
Pierre Harouimi 2022년 1월 25일
From R2021b, you can use pyrun fonction:
pyrun("a = b*c", b = 5, c = 10)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by