a question about websave

조회 수: 3 (최근 30일)
alpedhuez
alpedhuez 2022년 6월 18일
답변: Divyam 2025년 6월 11일
There is a webpage "test.com/data" and a file "2022_06_18_abdacadabra.xlsx" is posted on that webpage. There doeThe file name changes each day, like it was "2022_06_17_abdacadabra.xlsx." I want to be able to download this file using websave using the wildcard for the date part of the file name. How will it work?
  댓글 수: 2
Chris Burschyk
Chris Burschyk 2022년 6월 18일
Maybe something like this. Only works if the file is downloadable via that url. Instead of 'today' you can use any other date.
url = sprintf("https://www.test.com/data/%s_abdacadabra.xlsx",datetime('today',"Format","yyyy_MM_dd"))
url = "https://www.test.com/data/2022_06_18_abdacadabra.xlsx"
websave("PathToFile",url)
Error using websave
The connection to URL 'https://www.test.com/data/2022_06_18_abdacadabra.xlsx' timed out after 5.000 seconds. The reason is "Connection timeout after 5100 ms". Perhaps the server is not responding or
weboptions.Timeout needs to be set to a higher value.
alpedhuez
alpedhuez 2022년 6월 18일
Let me work on it.

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

답변 (1개)

Divyam
Divyam 2025년 6월 11일
The "websave" function does not natively support wildcards in URLs. To download a file where the file name changes daily, you can programmatically construct the file name for the date and then pass the URL to the function.
Assuming that the file is in the "YYYY_MM_DD_abdacadabra.xlsx" format, here is the sample code to perform the above operation:
% Get today's date
today = datetime('today');
% Format the date as 'YYYY_MM_DD'
dateStr = datestr(today, 'yyyy_mm_dd');
% Construct the filename
fileName = [dateStr '_abdacadabra.xlsx'];
% Construct the full URL
url = ['https://test.com/data/' fileName];
% Local file path to save
outFile = fullfile(pwd, fileName); % Save in current directory
% Download the file
try
websave(outFile, url);
fprintf('File downloaded successfully: %s\n', outFile);
catch ME
fprintf('Failed to download file: %s\n', ME.message);
end
Note: A good practice is to ensure using RESTful web service calls within try/catch blocks to avoid undetected errors.
For more information regarding the "websave" function, refer to the following documentation: https://www.mathworks.com/help/matlab/ref/websave.html

카테고리

Help CenterFile Exchange에서 Downloads에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by