can MATLAB download files from a website ?

Is it possible with MATLAB to download files from a website ? If the answer is yes, how do we proceed ?

Answers (4)

Carlton
Carlton on 1 Jun 2023
Moved: Image Analyst on 1 Jun 2023
Yes, it is possible to download files from a website using MATLAB. Here's a general approach you can follow:
  1. Use the MATLAB function webread or websave to download the file from the website. Both functions can handle HTTP and HTTPS requests.
  2. Determine the URL of the file you want to download. This could be a direct link to the file or a URL that triggers a file download.
  3. If you're using webread, you can use the following syntax to download the file:
matlab
url = 'https://www.example.com/path/to/file.txt';
data = webread(url);
  1. If you're using websave, you can specify the filename and path where the downloaded file should be saved. Here's an example:
matlab
url = 'https://www.example.com/path/to/file.txt';
filename = 'myfile.txt';
websave(filename, url);
  1. After executing the appropriate function, MATLAB will initiate the download process and save the file to the specified location. Make sure you have the necessary permissions to write files in the chosen directory.
Note: Some websites may require authentication or have restrictions on downloading files programmatically. In such cases, additional steps might be needed, such as providing login credentials or analyzing the website's API for download options.
It's important to review the terms and conditions of the website you're accessing to ensure that downloading files aligns with their policies.
Hi,
the function you are looking for is urlread
doc urlread
Titus

5 Comments

Or possibly urlwrite()
with urlread you can read content at URL. But you can't download a file from a website ?
I have the same problem, i want to download an .exe file from a website
but urlread and urlwrite don't download it
what do i do wrong?
url = 'https://github.com/FreeFem/FreeFem-sources/releases';
urlwrite (url, 'FreeFem.-4.4-2-win64.exe');
Untitled.jpg
I want download the file in red box.
urlwrite creates a file with the same name but its size = 152 kb and it doesn't work.
How can I download it?
thank you.
Hi,
I also need to download some compressed files from a website. I also tried this method but I got a corrupted file (web content) instead of the desired file.
Here, I solved and it works for me. You need to give the exact URL of the file in the command not just the URL of the page. You can try this code.
url = 'https://github.com/FreeFem/FreeFem-sources/releases/download/v4.8/FreeFEM-4.8-win7-64.exe';
urlwrite (url, 'FreeFEM-4.8-win7-64.exe');
In case anyone still needs to do this, websave is now the best function.

Sign in to comment.

I've made a batch file in MATLAB with the fprintf() function that is an ftp script (made up of just standard run-of-the-mill ftp commands), then use the system() function to run the ftp script.
Adrian
Adrian on 28 Aug 2023
Yes, MATLAB has the capability to download files from the web. One of the most commonly used functions to download data from the web is webread for retrieving web content and websave for downloading files. Here's how you can use these functions:1. Using websave:
The websave function is useful if you know the exact URL of the file you want to download.
matlabCopy code
% URL of the file to be downloaded url = 'https://example.com/path/to/your/file.txt'; % Specify the local path where you want to save the downloaded file localPath = 'C:\path\to\save\file.txt'; % Use websave to download the file filepath = websave(localPath, url); % Display the path to the saved file disp(['File saved to: ', filepath]);
2. Using webread:
If you're retrieving content rather than a file, you might use webread:
matlabCopy code
url = 'https://api.example.com/data'; data = webread(url); disp(data);
Important Considerations:
  1. Permissions: Ensure you have the necessary permissions to download the content from the website. Some websites may block automated requests or have terms of service that restrict scraping or downloading content.
  2. Web Options: Both webread and websave allow for additional web options to be set, such as setting request timeouts, user agents, or custom headers. You can create these options using weboptions.matlabCopy codeoptions = weboptions('Timeout', 60); % Set a timeout of 60 seconds data = webread(url, options);
  3. Errors & Exceptions: It's good practice to handle potential errors using try-catch blocks. Websites can change, move, or remove content, and your MATLAB code should be robust enough to handle these scenarios without crashing.
Remember to refer to MATLAB's official documentation for any updates or specific nuances about these functions.

1 Comment

hi @Adrian, I've the same problem but using both the functions I do not download the file.
With webread I save the HTML webpage content to a local variable, with websave I save the webpage as an HTML file.
How do I navigate among webpages, inserting username and password (if requested), searching for a file in the url specified, and downloading a file resident on that url (pdf, txt, xls, ...)?
Previously I used to create an ftp object and downloading the file with mget, but this doesn't work anymore.
Any suggestion would be really helpful.
Regards,
Marco

Sign in to comment.

Categories

Products

Asked:

on 16 Nov 2012

Community Treasure Hunt

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

Start Hunting!