How do I check if a file on a website exists?

7 vues (au cours des 30 derniers jours)
John
John le 24 Oct 2012
Commenté : Ali Abed le 15 Oct 2017
I want to read in image files from my online database.
I would like to check my website to see if the file exists before attempting to load it into the workspace.
For Example I would like to do (simplified):
E = exist('http://www.mywebsite.com/images/1.png);
if E ~= 0
IMG = imread('http://www.mywebsite.com/images/1.png);
else
IMG = zeros(X,Y);
end
Thanks in Advance!

Réponse acceptée

Sean de Wolski
Sean de Wolski le 24 Oct 2012
You could just use a try/catch block.
try
IMG = imread('http://www.mywebsite.com/images/1.png);
catch
IMG = zeros(X,Y);
end
  1 commentaire
John
John le 24 Oct 2012
Thanks for the fast response... I didn't know that try and catch blocks existed. This is going to help me a lot with loads of other codes!

Connectez-vous pour commenter.

Plus de réponses (1)

Erick Oberstar
Erick Oberstar le 15 Déc 2015
you can use urlread also
[str,status] = urlread('http://www.mywebsite.com/images/1.png');
if status
IMG = imread('http://www.mywebsite.com/images/1.png');
else
disp('URL read of link was unsuccessful')
end

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by