Extract data from excel using heading

I have my data in an excel file. I want matlab to extract data and make a matrix from 9 places that have the same heading and the same length. The data is all located in the same column in the excel file. The locations vary between different excel files, so I want to use the heading to find data in the file not the location.

 採用された回答

David Sanchez
David Sanchez 2014 年 6 月 9 日

0 投票

[data headings] = xlsread('your_excel.xls');
headings = headings(1,:);% first row of headings, in case you have some others.
head_wanted = 'your_desire_heading'; % heading of your desired column
col_desired = getnameidx(headings, head_wanted); % position of your wanted column
your_wanted_data = data(:,col_desired);

3 件のコメント

A T
A T 2014 年 6 月 9 日
The problem is that the data is all in the same column just starting from different rows (and I cannot use the position in the column, because this changes with different files). And the heading is always the same, the length of the data is also always the same.
A simplified example: in excel
A(column)
1(row) Data(heading)
2 5
3 7
4 2
5 Data
6 6
7 5
8 1
I want to get two vectors: (5 7 2) and (6 5 1)
David Sanchez
David Sanchez 2014 年 6 月 9 日
This could be valid for you then:
[data headings] = xlsread('your_ecel.xls');
heading_pos =[];
for k=1:numel(headings)
if strcmp(headings{k},'Data')
heading_pos = [heading_pos; k];
end
end
your_array = zeros(3,numel(heading_pos));
% valid for a long column with several Data headings/values
for k2=1:(numel(heading_pos)-1)
your_array(:,k2) = data(heading_pos(k2):(heading_pos(k2+1)-2));
end
your_array(:,end) = data(heading_pos(end):end);
you will end up with a 2D matrix holding your vector in each column
A T
A T 2014 年 6 月 11 日
Thank you!

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

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2014 年 6 月 9 日

0 投票

Use a table, that way you can index by variable name.
Attach an Excel sheet with specific instructions if you'd like a minimal working example.

タグ

質問済み:

A T
2014 年 6 月 9 日

コメント済み:

A T
2014 年 6 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by