« Teaching Geoscience with MATLAB Interest Group

'webread' for ingesting data  

This post was edited by Sarah Nakamoto on Jan, 2021
Hi All:

When Frederik shared his revelation on publish, he opened my eyes to webread.m to bring in data directly to workspace. His example used a .csv file, which webread brings in beautifully (although I'm not sure how it assigns those field names to the structure 'w'). However, when I try webread on the following ascii file:

w=webread(www.essie.ufl.edu/~arnoldo/ocp6168/homework/lapreg.dat);

it brings it in as a long string (~83 kilocharacters), that needs to be parsed, reshaped, and converted (str2num), to work with numeric values. I just want a flag that tells MATLAB (webread) to expect the data to come in as double-precision numbers, not a long continuous string. I've looked up the documentation for webread and weboptions, but I didn't see a straightforward solution here. Anybody experienced with this who can point out my (dumb) mistake?

thanks.
Pete

7652:25262

Share edittextuser=29480 post_id=25262 initial_post_id=0 thread_id=7652

This post was edited by Sarah Nakamoto on Jan, 2021
Peter,

Try to read it into a table instead:

w=webread('www.essie.ufl.edu/~arnoldo/ocp6168/homework/lapreg.dat',weboptions('ContentReader',@readtable))

though I note it will right now bring it into a table that is NX1 with 3 columns in the column itself. You can then extract the data into a double, turn it into 3 separate vectors, turn it into a 3 column table, ... Your choice.

--Loren

7652:25265

Share edittextuser=29492 post_id=25265 initial_post_id=0 thread_id=7652

but I also am having trouble - running into a bug. Trying to work around it now...

7652:25268

Share edittextuser=29492 post_id=25268 initial_post_id=0 thread_id=7652

This post was edited by Sarah Nakamoto on Jan, 2021
Here's the code I got to work:

options = weboptions('ContentReader',@(fn)readtable(fn, 'Format','%f %f %f',...
'HeaderLines',1,'ReadVariableNames',false));
w=webread(www.essie.ufl.edu/~arnoldo/ocp6168/homework/lapreg.dat, options);

%%
varnames = {'DayofYear', 'east', 'north'};
w.Properties.VariableNames = varnames;

7652:25271

Share edittextuser=29492 post_id=25271 initial_post_id=0 thread_id=7652

By the way, a great example using this is on my blog - written by my colleague, Kelly.

http://blogs.mathworks.com/loren/2014/12/31/using-restful-web-service-interfa...

He makes good use of ContentReader. Check it out.

7652:25274

Share edittextuser=29492 post_id=25274 initial_post_id=0 thread_id=7652

Join the Discussion


Log in to reply

« Teaching Geoscience with MATLAB Interest Group
Advertisement