Solved

Importing data from txt or Excel file in Matlab

Posted on 2009-07-12
10
2,207 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hibernate methods 2 71
tenRun challenge 28 98
withoutTen challenge 14 121
wordappend challenge 8 172
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 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