[Python] Matlab calling python, returning vector, str2num not working on returned vector

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a piece of Matlab code in which it makes a number of calls to python. These calls are returning the data as a string. The problem I am having is converting the string to a number. Its not as simple as that though, for two of my calls it converts the strings no problem but for this third one it only works if the vector does not exceed one line in the Matlab command window.

    My code is as follows:

    Call one

    Matlab:

    wind_direction=sprintf('python C:\\Users\\pduffy\\Documents\\MMI_Raptur_V2\\Python\\Distributions\\CDF.py %f %f %f %f %f %f %f %f %f', N,NE,E,SE,S,SW,W,NW,Iterations);
    [status,Wind_Dir_string] = system (wind_direction);
    Wind_Dir_shape=str2num(Wind_Dir_string);
    Wind_Dir= reshape(Wind_Dir_shape,[],1)


    Python

    import sys
    import scipy.stats


    def CDF_Random(N,NE,E,SE,S,SW,W,NW,Iterations):


    WindDir = (0,45,90,135,180,225,270,315)
    Freq=(N,NE,E,SE,S,SW,W,NW)

    cdf=scipy.stats.rv_discrete(values=(WindDir,Freq))

    cdf_rand=cdf.rvs(size=Iterations)
    return (cdf_rand)

    if __name__ == '__main__':
    N = float(sys.argv[1])
    NE = float(sys.argv[2])
    E = float(sys.argv[3])
    SE = float(sys.argv[4])
    S = float(sys.argv[5])
    SW = float(sys.argv[6])
    W = float(sys.argv[7])
    NW = float(sys.argv[8])
    Iterations = float(sys.argv[9])
    sys.stdout.write(str(CDF_Random(N,NE,E,SE,S,SW,W,NW,Iterations)))


    Call two

    Matlab

    fragment_velocity_string=sprintf('python C:\\Users\\pduffy\\Documents\\MMI_Raptur_V2\\Python\\Distributions\\beta.py %f %f %f %f', lower, mode, upper, Iterations);
    [status,Vf_rpm_string] = system (fragment_velocity_string);
    Vf_rpm_shape=str2num(Vf_rpm_string);
    Vf_rpm= reshape(Vf_rpm_shape,[],1)


    Python

    import sys
    import numpy.random

    def Beta_Random(lower,mode,upper,Iterations):

    Iterations=int(Iterations)
    mean=(1/6)*(lower+upper+(4*mode))
    a=((mean-lower)*((2*mode)-lower-upper))/((mode-mean)*(upper-lower))
    b=(a*(upper-mean))/(mean-lower)


    beta_rand=numpy.random.beta(a,b,[Iterations])

    rand_sample=(beta_rand*(upper-lower))+lower

    return (rand_sample)

    if __name__ == '__main__':
    lower = float(sys.argv[1])
    mode = float(sys.argv[2])
    upper = float(sys.argv[3])
    Iterations = float(sys.argv[4])
    sys.stdout.write(str(Beta_Random(lower,mode,upper,Iterations)))


    Call three the one causing me problems

    Matlab

    wind_speed_string=sprintf('python C:\\Users\\pduffy\\Documents\\MMI_Raptur_V2\\Python\\Distributions\\Weibull.py %f %f %f', alpha, beta, Iterations);
    [status,Vw_resultant_string] = system (wind_speed_string) ;
    Vw_shape=str2num(Vw_resultant_string);
    Vw_resultant= reshape(Vw_shape,[],1)


    Python

    import sys
    import numpy.random

    def Weibull_Random(alpha,beta,Iterations):



    a=alpha
    b=beta


    weibull_rand=numpy.random.weibull(a,[Iterations])

    rand_sample=((weibull_rand)*b)

    return (rand_sample)

    if __name__ == '__main__':
    alpha = float(sys.argv[1])
    beta = float(sys.argv[2])
    Iterations = float(sys.argv[3])
    sys.stdout.write(str(Weibull_Random(alpha,beta,Iterations)))


    So what ever number I set Iterations to, this number of values is returned to Matlab. In all three Matlab codes I use str2numto covert the data received from Python and then reshape it to create a single column matrix.

    For the third piece of code, the one causing me problems, I have tried rounding the number in python to four decimal places using numpy.around. I chose four as this is the same as Matlab converts to using str2num. I aslo tried using str2double but got a single NAN value returned.

    So as I said Matlab will convert all the received data from python to a number for any amount of Iterations for the first and secound piece of code. But will only work when the vector does not exceed one line for the third peice of code, so at the moment it only works for Iterations = 6

    This seems very strange to me as all the sections of code are very similar except the functions they preform in Python.

    I'm using windows 7, Matlab 2012, Python 3 with Numpy and Scipy (pyzo2013b)

    Continue reading...

Compartilhe esta Página