MATLAB: (Download image) download image If I have a txt file, which contains the url that downloads images, how do we write the script to do that? e.g. txt file information: .. .. … Best Answer projectdir = fullfile(pwd, 'saved_images'); %location to store the filesif ~exist(projectdir, 'dir') mkdir(projectdir);endfid = fopen('TheFile.txt', 'rt');imcount = 0;while true this_url = fgetl(fid); if ~ischar(this_url) break; %end of file end if isempty(strtrim(this_url)) continue %empty line, probably near end of file end [url_path, url_img, url_ext] = fileparts(this_url); dest_file = fullfile(projectdir, [url_img url_ext]); if exist(dest_file, 'file') fprintf('skipping existing %s\n', dest_file); imcount = imcount + 1; urls{imcount} = this_url; img_files{imcount} = dest_file; %img_content{imcount} = imread(dest_file); continue; end fprintf('loading ... %s\n', url_img); try urlwrite(this_url, dest_file); imcount = imcount + 1; urls{imcount} = this_url; img_files{imcount} = dest_file; %img_content{imcount} = imread(dest_file); catch fprintf('failed downloading %s from "%s"\n', url_img, this_url); endend Related SolutionsMATLAB: Save a .txt file Did you check the help for fgetl()?fid = fopen('fgetl.m');urlString= fgetl(fid);while ischar(urlString) disp(urlString) urlString = fgetl(fid); webPageContents = urlread(urlString); % Save it, or whatever.....endfclose(fid); MATLAB: Save the result of urlread as file in current directory instead of a variable in the workspace You can save the file using urlwrite instead of urlread:file = fullfile(cd, 'TheZipFile.zip');urlwrite(['http://www.ecb.europa.eu/stats/eurofxref/eurofxref-', ... 'hist.zip?2d1147c9cf376b26c445ffee4d2b37d1'], ... file);unzip(file);urlread replies the contents of the read file as a variable, but unzip expects a file name. Related QuestionIs there a simple way to obtain a table with the number of pixels for each image in a folderHow to read and resize and change color image from URL link in our MATLAB commands
Best Answer