Importing | delimited data from Excel .xls and "Text to Columns"
Show older comments
Hello, I am trying to import data from Excel (.xls). This data has a single column of mixed (numeric and string) data that is | (bar) delimited. My goal is to import this data and seperate each delimited portion into a seperate columns while maintaining the rows. Essentially, I am trying to do the Excel "Text to columns" with MATLAB. Thank you
Answers (2)
Bob Hamans
on 14 Jun 2011
Have a look at the importdata function:
filename='myfile.xls';
A = importdata(filename,'|');
6 Comments
Fangjun Jiang
on 14 Jun 2011
Good to know this argument. But I tried both .xls file and .txt file, it still read the whole string. See my answer below for example.
Bob Hamans
on 15 Jun 2011
Hi Fangjun what Matlab version are you using? Unfortunately I cannot access the archived documentation to check the documentation for older Matlab releases and the functionality of importdata as my license does not allow for that. See http://www.mathworks.com/help/doc-archives.html and check for your version. I would be interested to hear if something changed and broke backward/forward compatibility of this function.
Fangjun Jiang
on 15 Jun 2011
I have R2007b and R2010b. In R2007b, the result is like the one shown in my answer. In R2010b, it is like below so it should work for Brian.
x =
data: [3x1 double]
textdata: {3x1 cell}
rowheaders: {3x1 cell}
>> x.data
ans =
1
2
3
>> x.textdata
ans =
'a '
'b '
'c '
Brian
on 15 Jun 2011
Fangjun Jiang
on 15 Jun 2011
I tested on R2010b and the results are same as yours. I have no idea. Maybe importdata() is not smart enough to handle inconsistent data format (the 4th line is certainly different than the first 3 lnies).
You need to provide a sample of data that is representative of your real data. In your comments on your question, you have consecutive delimiters and no numeric data. In your example above, you have lines that have different numbers of delimiters. What is your real data look like? What is your expected output?
Fangjun Jiang
on 15 Jun 2011
Maybe the following text form help importdata() will help.
For ASCII files and spreadsheets, IMPORTDATA expects to find numeric data in a rectangular form (that is, like a matrix). Text headers can
appear above or to the left of numeric data. To import ASCII files
with numeric characters anywhere else, including columns of character
data or formatted dates or times, use TEXTSCAN instead of IMPORTDATA.
When importing spreadsheets with columns of nonnumeric data, IMPORTDATA cannot always correctly interpret the column and row headers.
Fangjun Jiang
on 14 Jun 2011
Let's say my test.txt is like below
a | 1
b | 2
c | 3
x=importdata('test.txt','|')
x =
'a | 1'
'b | 2'
'c | 3'
Then you need to do some processing to get the data type you want.
x1=regexprep(x,'\|.+','')
x2=regexprep(x,'.+\|','')
y1=deblank(x1)
y2=str2num(char(x2))
7 Comments
Brian
on 14 Jun 2011
Fangjun Jiang
on 14 Jun 2011
Well, if your original file is in .txt file in my example, you can use
[y1,y2]=textread('test.txt','%s%d','delimiter','|') to get it. If your original file is in .xls file, then you have to use xlsread() or importdata() to get the data in a format like x above and then do porcessing.
Brian
on 14 Jun 2011
Fangjun Jiang
on 14 Jun 2011
What is your text file look like? Can you post an example? I ran the command and it didn't have any problem. Also, type help textread to look at the calling syntax of textread.
Brian
on 15 Jun 2011
Fangjun Jiang
on 15 Jun 2011
That must be that we are using different version of Matlab. I am using R2007b. You must use a new version. It looks like importdata('test.txt','|') could work as Bob Hamans suggested. Type x.data and x.textdata to see if that meets your need.
Bob Hamans
on 15 Jun 2011
I am still on R2008a here. Brian did I understand correctly my solution does work for you? Did you check the contents of x.data?
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!