Error using websave (line 98)

The purpose of this code is to download the .nc4 data files.
I'm using the following code on my Linux Mint, and I'm encountering this error:
Error using websave (line 98)
Execution of script HTTPConnector as a function is not supported:
/usr/local/MATLAB/R2021a/toolbox/matlab/external/interfaces/webservices/restful/+matlab/+internal/+webservices/HTTPConnector.m
My code:
% Anos e meses correspondentes aos links
years = ['1990', '1990', '1990'];
months = ['01', '02', '03'];
% Loop para baixar os dados
for i = 1:length(years)
year = years(i);
month = months(i);
url = sprintf(['https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2_DIURNAL/M2IUNXASM.5.12.4/',...
year, '/MERRA2_100.instU_2d_asm_Nx.', year, month, '.nc4.nc4?',...
'QV10M[0:23][141:206][148:246],time,lat[141:206],lon[148:246]']);
filename = ['MERRA2_', year, month, '.nc4.nc4'];
disp(['Baixando dados de ', year, '-', month]);
websave(filename, url);
disp(['Dados de ', year, '-', month, ' baixados e salvos como ', filename]);
end

4 Commenti

The following comment doesn't address the error with websave/HTTPConnector, but I believe you got completely confused in how you define years and months.
Did you mean to define years and months as cell arrays, like this?
% Anos e meses correspondentes aos links
years = {'1990', '1990', '1990'}
years = 1×3 cell array
{'1990'} {'1990'} {'1990'}
months = {'01', '02', '03'}
months = 1×3 cell array
{'01'} {'02'} {'03'}
% Loop para baixar os dados
for i = 1:length(years)
year = years{i};
month = months{i};
% url = sprintf(['https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2_DIURNAL/M2IUNXASM.5.12.4/',...
% year, '/MERRA2_100.instU_2d_asm_Nx.', year, month, '.nc4.nc4?',...
% 'QV10M[0:23][141:206][148:246],time,lat[141:206],lon[148:246]']);
filename = ['MERRA2_', year, month, '.nc4.nc4'];
disp(['Baixando dados de ', year, '-', month]);
% websave(filename, url);
disp(['Dados de ', year, '-', month, ' baixados e salvos como ', filename]);
end
Baixando dados de 1990-01
Dados de 1990-01 baixados e salvos como MERRA2_199001.nc4.nc4
Baixando dados de 1990-02
Dados de 1990-02 baixados e salvos como MERRA2_199002.nc4.nc4
Baixando dados de 1990-03
Dados de 1990-03 baixados e salvos como MERRA2_199003.nc4.nc4
Or did you mean to define them as character arrays, like you did?
% Anos e meses correspondentes aos links
years = ['1990', '1990', '1990']
years = '199019901990'
months = ['01', '02', '03']
months = '010203'
% Loop para baixar os dados
for i = 1:length(years)
year = years(i);
month = months(i);
% url = sprintf(['https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2_DIURNAL/M2IUNXASM.5.12.4/',...
% year, '/MERRA2_100.instU_2d_asm_Nx.', year, month, '.nc4.nc4?',...
% 'QV10M[0:23][141:206][148:246],time,lat[141:206],lon[148:246]']);
filename = ['MERRA2_', year, month, '.nc4.nc4'];
disp(['Baixando dados de ', year, '-', month]);
% websave(filename, url);
disp(['Dados de ', year, '-', month, ' baixados e salvos como ', filename]);
end
Baixando dados de 1-0
Dados de 1-0 baixados e salvos como MERRA2_10.nc4.nc4
Baixando dados de 9-1
Dados de 9-1 baixados e salvos como MERRA2_91.nc4.nc4
Baixando dados de 9-0
Dados de 9-0 baixados e salvos como MERRA2_90.nc4.nc4
Baixando dados de 0-2
Dados de 0-2 baixados e salvos como MERRA2_02.nc4.nc4
Baixando dados de 1-0
Dados de 1-0 baixados e salvos como MERRA2_10.nc4.nc4
Baixando dados de 9-3
Dados de 9-3 baixados e salvos como MERRA2_93.nc4.nc4
Index exceeds the number of array elements. Index must not exceed 6.

'months' appears to be both a function and a variable. If this is unintentional, use 'clear months' to remove the variable 'months' from the workspace.
Take a close look at the outputs produced by disp in both cases, the way you have it doesn't make much sense (and produces an error because months is shorter than years).
Now remove the comments from the following lines and check if the data is being downloaded properly with your new suggestion.
url = sprintf(['https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2_DIURNAL/M2IUNXASM.5.12.4/',...
year, '/MERRA2_100.instU_2d_asm_Nx.', year, month, '.nc4.nc4?',...
'QV10M[0:23][141:206][148:246],time,lat[141:206],lon[148:246]']);
and
websave(filename, url);
Voss
Voss il 10 Ago 2023
Modificato: Voss il 10 Ago 2023
"check if the data is being downloaded properly with your new suggestion."
As I stated clearly: "The following comment doesn't address the error with websave/HTTPConnector".
It's a question of whether you want to try to access
year = '1990'; % my suggestion
month = '01';
url = sprintf(['https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2_DIURNAL/M2IUNXASM.5.12.4/',...
year, '/MERRA2_100.instU_2d_asm_Nx.', year, month, '.nc4.nc4?',...
'QV10M[0:23][141:206][148:246],time,lat[141:206],lon[148:246]'])
url = 'https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2_DIURNAL/M2IUNXASM.5.12.4/1990/MERRA2_100.instU_2d_asm_Nx.199001.nc4.nc4?QV10M[0:23][141:206][148:246],time,lat[141:206],lon[148:246]'
or
year = '1'; % your way
month = '0';
url = sprintf(['https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2_DIURNAL/M2IUNXASM.5.12.4/',...
year, '/MERRA2_100.instU_2d_asm_Nx.', year, month, '.nc4.nc4?',...
'QV10M[0:23][141:206][148:246],time,lat[141:206],lon[148:246]'])
url = 'https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2_DIURNAL/M2IUNXASM.5.12.4/1/MERRA2_100.instU_2d_asm_Nx.10.nc4.nc4?QV10M[0:23][141:206][148:246],time,lat[141:206],lon[148:246]'
etc.

Accedi per commentare.

Risposte (1)

/usr/local/MATLAB/R2021a/toolbox/matlab/external/interfaces/webservices/restful/+matlab/+internal/+webservices/HTTPConnector.m should not be a script. It should start with
%matlab.internal.webservices.HTTPConnector HTTP connector handle object
and the first non-comment line should be
classdef HTTPConnector < handle
If you are lucky, you have simply accidentally editted it like inserting a couple of characters right before the initial % that you can just remove. But otherwise you will need to restore the file from backup or reinstall MATLAB.

2 Commenti

I believe you got completely confused in your response. Take a close look at what you wrote, your answer doesn't make much sense in relation to my question.
You posted
Error using websave (line 98)
Execution of script HTTPConnector as a function is not supported:
/usr/local/MATLAB/R2021a/toolbox/matlab/external/interfaces/webservices/restful/+matlab/+internal/+webservices/HTTPConnector.m
That tells us that your call to websave() is internally calling matlab.internal.webservices.HTTPConnector as-if it were a function (more exactly, expecting it to be a constructor), but that the implementing file /usr/local/MATLAB/R2021a/toolbox/matlab/external/interfaces/webservices/restful/+matlab/+internal/+webservices/HTTPConnector.m is instead a script. That is, that the implementing file has something potentially executable before the first "function" or "classdef" statement.
I examined the executable file, which happens to be at location /Applications/MATLAB_R2021a.app/toolbox/matlab/external/interfaces/webservices/restful/+matlab/+internal/+webservices/HTTPConnector.m on my system. The first line of the file is the comment I indicated, and that is followed by comments and empty lines until it reaches the "classdef" line that I indicated.
We can tell from the error message that the error is not in your lines of code, and is not in websave(): that instead your /usr/local/MATLAB/R2021a/toolbox/matlab/external/interfaces/webservices/restful/+matlab/+internal/+webservices/HTTPConnector.m is corrupted relative to what it should be.
If you are lucky then your /usr/local/MATLAB/R2021a/toolbox/matlab/external/interfaces/webservices/restful/+matlab/+internal/+webservices/HTTPConnector.m file has a few extra characters at the beginning that you can edit out. If you are not so lucky then you will need to restore that file from backups or reinstall MATLAB.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by