I am having some problems reading data from a database in Matlab. The url link I use to download the data gives a semicolon delimited text file and I need Matlab to identify this data and arrange it accordingly in a struct
format (since the data has different classes) for example. I have already used urlread
and could download the data succesfully, the only problem is I am getting all the data as a character string inside one cell and I need to get this data as a table and well organized.
Basically I would like to know if it is possible to load data from a url into Matlab the same way the read.csv
function in R does, where you just put the url where the file name should go and define how the data is delimited and viola, you get your data.frame
with all your data perfectly organized as it should.
I suppose there are ways to interpret the character string after using urlread
and convert it somehow into an organized struct
variable but there has to be a way to read it directly from the url as R does.
Here is a piece of code that will read the csv
data from the web ( urlread
), use textscan
to scan and format the data into cells (strings and scalars allowed), then convert the cells into a structure with cell2struct
. The structure created keeps the textscan
formatting.
Note that you have to define the textscan
format and the cell2struct
input to suit your data.
block = urlread('http://hci.stanford.edu/jheer/workshop/data/florida2000/Florida2000.csv');
C = textscan(block,'%s%s%f%s%f','HeaderLines',1,'EndOfLine','\n');
S = cell2struct(C,{'county','technology','columns','category','ballots'},2)
Here is the Florida 2000 Presidential Election Results ( .csv
, 938 data points)
county,technology,columns,category,ballots
Alachua,Optical,1,under,217
Alachua,Optical,1,over,105
Alachua,Optical,1,Bush,34124
Alachua,Optical,1,Gore,47365
Alachua,Optical,1,Browne,658
Alachua,Optical,1,Nader,3226
Alachua,Optical,1,Harris,6
...
that will produce
S =
county: {938x1 cell} %string
technology: {938x1 cell} %string
columns: [938x1 double] %double
category: {938x1 cell} %string
ballots: [938x1 double] %double
Edit
For double quoted text, you can use %q
instead of %s
in calling textscan
( FormatSpec options ) just like that
C = textscan(fileID,'%q%f');
Look into a function called dlmread
. This will allow you to put in a string of data, tell it what the delimiter is, and it should pump out what you need.
results = dlmread('http://someurl.com/somefile.txt',';')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.