importdata no recognizing column headers

7 ビュー (過去 30 日間)
hxh103
hxh103 2014 年 12 月 1 日
編集済み: hxh103 2014 年 12 月 1 日
I am using MATLAB 2012a on Windwos 7. I am trying to import a tab separated text file. The problem is that importdata does not recognize columnheaders if the length of the data is different from the length of the columnheaders. The program I am using is outputting an extra header eventhough I have no data for it. I can't change the output and there are about 100 columns. i want to be able to search for a specific column to get the right index to read only the relevant data. Here is a small example of the text file (spaces are tabs). so in this example, there are 4 headers, but only 3 columns of data. Matlab does not recognize the column headers and won't put them into columns.
header1 header2 header2 header4
1 2 3
2 3 2
3 4 5
1 2 3
3 4 5
2 4 5
any solutions out there? I don't want to go into each file to delete the extra columns (it does work but I have hundreds of these outputs). One solution would be to go into the text data and rewrite the columns that way. but I'm hoping there is an easier and faster method out there...
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 1 日
Do you want to read the headers (all of them)
hxh103
hxh103 2014 年 12 月 1 日
yes, I would like to read all available headers

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 1 日
s=importdata('file.txt')
h=strsplit(s.textdata{:})
data=s.data
n=size(data,2)
out=[h(1:n);num2cell(data)]
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 1 日
h=regexp(s.textdata,'\S+','match');
h=h{:}
hxh103
hxh103 2014 年 12 月 1 日
編集済み: hxh103 2014 年 12 月 1 日
Sorry, I forgot to mention there are other text above the column headers. so the above code won't work as it doesn't give the correct headers. The following code works for me
s=importdata('file.txt')
t=s.textdata{end,1}; %assumes your header is in the last row
h=regexp(t,'\t','split')
h= 'header1' 'header2' 'header3' 'header4'
Cheers, hope this helps someone else out in similar situation.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by