如何在matlab中执行python函数?

时间:2019-08-07 07:36:26

标签: python matlab

我有一些matlab代码来运行我创建的python函数。当我在Atom编辑器上单独运行python函数时,它可以工作。但是,当我尝试在matlab上运行它时,会显示一条错误消息。代码如下:

try
    vPy = pyversion;
    if isempty(vPy)
    %% Python plots

        pyversion("C:\ProgramData\Anaconda3\python.exe")
    end
    latitude = 43;
    longitude = 1.5;
    py.copernicus_request.CopernicusRequest(latitude, longitude);
catch 
end

这是python代码,可能对您不起作用,因为您需要创建一个用户来运行CDS api。

def CopernicusRequest(latitude, longitude):
    import cdsapi

    c = cdsapi.Client()

    #latitude = 43.1 # North, South
    #longitude = -1.5 # West , East
        #str(latitude)+'/'+str(longitude)+'/'+str(latitude)+'/'+str(longitude)
    r = c.retrieve(
            'reanalysis-era5-single-levels',
            {
                'product_type':'reanalysis',
                'variable':[
                    '100m_u_component_of_wind','100m_v_component_of_wind','10m_u_component_of_wind','10m_v_component_of_wind','2m_temperature',
                    'surface_pressure'
                ],
                'area'    : str(latitude)+'/'+str(longitude)+'/'+str(latitude)+'/'+str(longitude), # North, West, South, East. Default: global
                'year':'2018',
                'grid':'0.1/0.1', # Latitude/longitude grid in degrees: east-west (longitude) and north-south resolution (latitude). Default: reduced Gaussian grid
                'month':'01',
                'day':[
                    '01','02','03',
                    '04','05','06',
                    '07','08','09',
                    '10','11','12',
                    '13','14','15',
                    '16','17','18',
                    '19','20','21',
                    '22','23','24',
                    '25','26','27',
                    '28','29','30',
                    '31'
                ],
                'time':[
                    '00:00','01:00','02:00',
                    '03:00','04:00','05:00',
                    '06:00','07:00','08:00',
                    '09:00','10:00','11:00',
                    '12:00','13:00','14:00',
                    '15:00','16:00','17:00',
                    '18:00','19:00','20:00',
                    '21:00','22:00','23:00'
                ],
                'format':'netcdf'
            }
            )
    request_finished = 1

    r.download('download_grid_reduction_one_month_point_limit.nc')
    download_finished = 1
    return request_finished, download_finished


#CopernicusRequest(43.1, -1.5)

当我运行以下代码行时,我在matlab上遇到以下错误:

>> py.copernicus_request.CopernicusRequest(latitude, longitude)
Error using _tqdm>moveto (line 1269)
Python Error: AttributeError: 'NoneType' object has no attribute 'write'

Error in _tqdm>display (line 1314)
            self.moveto(pos)

Error in _tqdm>__init__ (line 945)
                self.display()

Error in api>_download (line 125)
                          leave=False,

Error in api>download (line 167)
                              target)

Error in copernicus_request>CopernicusRequest (line 49)
    r.download('download_grid_reduction_one_month_point_limit.nc')

是出现错误的那一行。

0 个答案:

没有答案