importing data with time in MATLAB

2019-08-27 21:51发布

how can I import the following text data file in MATLAB:

day    time        price       volume
01     8:00:06    29.0000      1000
01     8:00:06    29.1000       200
01     8:02:08    29.0700      1000
01     8:03:12    29.0000      1000

I tried the following code but doesn't work:

fid = fopen('data.txt');
dateItem = [];
values = [];
while ~feof(fid)
  [dateItem]= [dateItem ; fscanf(fid,'%s %s',2)]; 
  [values]= [values ; fscanf(fid,'%f %f',2)];
end
fclose(fid);

2条回答
Ridiculous、
2楼-- · 2019-08-27 22:18

a lot of small fixes,

but this would be a first step

values = [];
dateItem= char(dateItem, fscanf(fid,'%s %s %s %s',4)); %If you don't want this remove the equal statement  but keep fscanf
while ~feof(fid)
  dateItem = char(dateItem, [fscanf(fid,'%s',1),' ', fscanf(fid,'%s',1)]); 
  values= [values; (fscanf(fid,'%f%f',2))'];
end
fclose(fid);
查看更多
相关推荐>>
3楼-- · 2019-08-27 22:25

Typing 'help importdata' at the terminal will bring up useful information on how to use the importdata function. Alternatively, you can use the Import Data feature on your home menu (it's a big button for MATLAB 2013, it's on a menu bar for previous versions) which can generate code for importing your specific file layout.

查看更多
登录 后发表回答