I Just Want To Read Text From URL And Save It As Txt.... But How?

3 vues (au cours des 30 derniers jours)
Tyann Hardyn
Tyann Hardyn le 27 Mai 2024
Commenté : Mathieu NOE le 28 Mai 2024
Hello, Community
I just want to ask about how to read text data from URL and save it as ASCII / .Txt Data. It maybe a simple question for all of you, but it kinda so difficult for me...
So i have a data from URL like this :
2024 05 21 33744 33744.5 2602 4 1.667 1.000 1.000 2.667 2.667 2.333 1.667 1.667 6 4 4 12 12 9 6 6 7 165 190.8 195.4 0 2024 05 22 33745 33745.5 2602 5 1.667 0.667 0.333 0.333 0.000 0.667 1.333 1.333 6 3 2 2 0 3 5 5 3 152 195.7 200.6 0 2024 05 23 33746 33746.5 2602 6 0.667 1.667 1.667 2.333 2.333 1.667 2.333 4.000 3 6 6 9 9 6 9 27 9 134 176.2 180.7 0 2024 05 24 33747 33747.5 2602 7 2.000 3.000 2.667 3.000 2.000 1.333 0.333 1.333 7 15 12 15 7 5 2 5 8 126 162.8 167.0 0 2024 05 25 33748 33748.5 2602 8 1.667 1.333 1.333 0.667 0.667 1.667 1.333 2.333 6 5 5 3 3 6 5 9 5 104 152.4 156.4 0 2024 05 26 33749 33749.5 2602 9 1.333 2.333 1.000 2.000 2.667 2.667 2.667 1.000 5 9 4 7 12 12 12 4 8 120 155.6 159.7 0
Or like this in the image of the source:
The data result of webread function is just like this :
textweb = webread("https://kp.gfz-potsdam.de/kpdata?startdate=2024-05-21&enddate=2024-05-26&format=kp1#kpdatadownload-143");
textweb =
'2024 05 21 33744 33744.5 2602 4 1.667 1.000 1.000 2.667 2.667 2.333 1.667 1.667 6 4 4 12 12 9 6 6 7 165 190.8 195.4 0
2024 05 22 33745 33745.5 2602 5 1.667 0.667 0.333 0.333 0.000 0.667 1.333 1.333 6 3 2 2 0 3 5 5 3 152 195.7 200.6 0
2024 05 23 33746 33746.5 2602 6 0.667 1.667 1.667 2.333 2.333 1.667 2.333 4.000 3 6 6 9 9 6 9 27 9 134 176.2 180.7 0
2024 05 24 33747 33747.5 2602 7 2.000 3.000 2.667 3.000 2.000 1.333 0.333 1.333 7 15 12 15 7 5 2 5 8 126 162.8 167.0 0
2024 05 25 33748 33748.5 2602 8 1.667 1.333 1.333 0.667 0.667 1.667 1.333 2.333 6 5 5 3 3 6 5 9 5 104 152.4 156.4 0
2024 05 26 33749 33749.5 2602 9 1.333 2.333 1.000 2.000 2.667 2.667 2.667 1.000 5 9 4 7 12 12 12 4 8 120 155.6 159.7 0
'
And i want to know how to convert the output of webread function above to become a data like this :
"2024 05 21 33744 33744.5 2602 4 1.667 1.000 1.000 2.667 2.667 2.333 1.667 1.667 6 4 4 12 12 9 6 6 7 165 190.8 195.4 0"
"2024 05 22 33745 33745.5 2602 5 1.667 0.667 0.333 0.333 0.000 0.667 1.333 1.333 6 3 2 2 0 3 5 5 3 152 195.7 200.6 0"
"2024 05 23 33746 33746.5 2602 6 0.667 1.667 1.667 2.333 2.333 1.667 2.333 4.000 3 6 6 9 9 6 9 27 9 134 176.2 180.7 0"
"2024 05 24 33747 33747.5 2602 7 2.000 3.000 2.667 3.000 2.000 1.333 0.333 1.333 7 15 12 15 7 5 2 5 8 126 162.8 167.0 0"
"2024 05 25 33748 33748.5 2602 8 1.667 1.333 1.333 0.667 0.667 1.667 1.333 2.333 6 5 5 3 3 6 5 9 5 104 152.4 156.4 0"
"2024 05 26 33749 33749.5 2602 9 1.333 2.333 1.000 2.000 2.667 2.667 2.667 1.000 5 9 4 7 12 12 12 4 8 120 155.6 159.7 0"
OR just simply save the output of webread function to become a txt file (ASCII) which can be opened in my local PC...
Thank you everyone for your attention and cooperation. I would be very grateful if community can help me to solve this problem /.\ /.\ /.\

Réponse acceptée

Mathieu NOE
Mathieu NOE le 27 Mai 2024
hello
try this
textweb = webread("https://kp.gfz-potsdam.de/kpdata?startdate=2024-05-21&enddate=2024-05-26&format=kp1#kpdatadownload-143");
% save char array to txt file :
filename = 'myTextFile.txt';
% Open the file for writing using fopen:
fid = fopen(filename, 'w'); % Open the file for writing (overwrite if necessary)
% Write the char array to the file using fprintf. In this case, we interpret newline characters as new lines:
fprintf(fid, '%s', textweb);
% Close the file using fclose (this step is important!):
fclose(fid);
  6 commentaires
Tyann Hardyn
Tyann Hardyn le 28 Mai 2024
Thank you so much, Stephen23, thats very helpful too
Mathieu NOE
Mathieu NOE le 28 Mai 2024
yes indeed, but only for R2022a and above !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by