MATLAB Answers

Bob Choy

Save a .txt file

Asked by Bob Choy
on 15 Dec 2012

Hey, I need help on the following:

I have a .txt file with many URLs where each line is a diferent URL. Each URL contains only a bunch of text and numbers, nothing else. How do I create a function that reads that same .txt file, accesses each one those URLs and saves to my desktop a .txt that contains the data from that URL. One .txt file per URL.

Thank you for you time.




No products are associated with this question.

1 Answer

Answer by Image Analyst
on 15 Dec 2012
 Accepted answer

Did you check the help for fgetl()?

fid = fopen('fgetl.m');
urlString= fgetl(fid);
while ischar(urlString)
    urlString = fgetl(fid);
    webPageContents = urlread(urlString);
    % Save it, or whatever.....


You need to create a new filename each iteration, something like this:

fid = fopen(Name_File);
count = 1;
while ~feof(fid)
	URL = fgetl(fid);
        Name_FileLocal = sprintf('web page %d.txt', count);
        count = count+1;
	Contents = urlwrite(URL,Name_FileLocal);

Thats EXACTLY what I needed. Thank you good sir!

Question answered.

Go ahead and mark it so.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!