Solved

Importing data from txt or Excel file in Matlab

Posted on 2009-07-12
10
2,190 Views
Last Modified: 2016-02-10
Hi,
I am trying to import data into Matlab. There are five columns: Date, Time, Open, High, Low, Last Price. I am trying to do this through "uiimport" (import wizard) or "xlsread" command. The problem is that Matlab reads the Date and Time column as "Textdata" even though the data is in Date/Time format. How can I convert the data into Date/Time within Matlab? I have attached a sample txt file. Please advise. Thanks in advance
gbp-samp.txt
0
Comment
Question by:bose3
  • 5
  • 4
10 Comments
 
LVL 9

Expert Comment

by:masheik
ID: 24838473
hi bose,
%read and display excel data
[data, textString] = xlsread('gbp-samp.xls');

here data represents only numeric data,and textString represents only strings in cell array

make sure that  the data is on  correct sheet number .If it is not please mention the sheet number like this,
[data, textString] = xlsread('gbp-samp.xls','sheetName');

for more information on xlsread refer here,

http://www.sgr.nada.kth.se/unix/software/matlab/Release_14.1/techdoc/ref/xlsread.html
0
 

Author Comment

by:bose3
ID: 24838629
The data is in a correct tab since there is only one tab in the whole spreadsheet. I have attached  the Excel file sample aslo.
gbp-samp.xlsx
0
 
LVL 9

Expert Comment

by:masheik
ID: 24838750
i dont have matlab to check . can you show the output?
0
 

Author Comment

by:bose3
ID: 24838819
I have posted two pics, one for the text data and the other is for numeric data. see attachment

numeric-data.PNG
text-data.PNG
0
 
LVL 9

Expert Comment

by:masheik
ID: 24838834
this link can help you,
go to remarks and Handling Excel date values
http://matlab.izmiran.ru/help/techdoc/ref/xlsread.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bose3
ID: 24843477
I tried using ths script but no luck yet
[num, txt]=xlsread(GBP.xls);
tday1=txt(2:end, 1);
datestr(datenum(tday1, mm/dd/yyyy), mm/dd/yy);
tday1=str2double(cellstr(tday1));
0
 
LVL 9

Expert Comment

by:masheik
ID: 24846587
as i already mentioned, i do not have matlab to check
can you check this out,
[xlsDates] = xlsread('test.xls','A2:A21');
mDates = datenum('30-Dec-1899') + xlsDates;
datestr(mDates,2)
0
 

Author Comment

by:bose3
ID: 24851798
No luck with your script but I managed to find this http://www.mathworks.com/matlabcentral/fileexchange/18430. It seperates MM DD YYYY in seperate columns and keep them as numeric. Is there a way to join the columns after a split?
0
 
LVL 15

Accepted Solution

by:
yuk99 earned 50 total points
ID: 24853137
Try the code below and check D structure.
D=importdata('gbp-samp.txt');

D.date = datenum(cell2mat([D.textdata(2:end,1) repmat({' '},size(D.data,1),1) D.textdata(2:end,2)]));

D.datestr = datestr(D.date,0);

D = rmfield(D,'textdata');

Open in new window

0
 

Author Closing Comment

by:bose3
ID: 31602589
Awesome. Good work
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bigDiff challenge 17 84
tenRun challenge 28 92
canBalance challenge 34 70
squareUp  challenge 22 133
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now