How to extract certain Excel columns?
174 ビュー (過去 30 日間)
古いコメントを表示
I would like to extract columns based on their name (the first row of the column). These columns are randomly ordered.
0 件のコメント
回答 (3 件)
Matías Nomboly
2018 年 2 月 16 日
編集済み: Matías Nomboly
2018 年 2 月 16 日
Best quick thing I can think of is to use read table and then use MATLAB tables to classify items. Don't know about the efficiency of it, but it should work:
A=readtable('yourexcelfile.xlsx');
A then behaves as a cell structure, and thus you can use the function find(ismember())
coln=find(ismember(A(1,:),'yourstring'));
This returns the number of the column of the string that you are searching for Afterwards, you can recover your column by:
Anew=A(:,coln);
Hope this helps!
3 件のコメント
Matías Nomboly
2018 年 2 月 16 日
OK, lets try this>
A=readtable('yourexcelfile.xlsx');
A=table2array(A);
coln=find(ismember(A(1,:),'yourstring'));
Anew=A(:,coln);
Guillaume
2018 年 2 月 16 日
ismember is a bit overkill here. find(strcmp(x, 'yourstring') would produce the same result faster.
The main issue here is that the x should not be A(1, :) since row headers are not stored in the table values. They're used a variable nams and can be accessed through A.Properties.VariableNames if needed.
However, the whole thing is overkill, you can access the column by its name directly. That's the whole point of tables.
Guillaume
2018 年 2 月 16 日
編集済み: Guillaume
2018 年 2 月 16 日
Using readtable is indeed the right way to do it. By default, readtable should read the first row as variable names. If it doesn't then you can force it with:
t = readtable(yourfile, 'ReadVariableNames', true);
Be aware that matlab may have to change the column name slightly to make it a valid variable name (this is the warning you see), so you may have to adjust your search term if it's not a valid variable name.
You can then access the column that interest you very simply with:
t.yourcolumnname
e.g. if you're looking for a column whose header in excel is MeanValue simply do:
t.MeanValue
to get that column content. However, as said if the column header is Mean Value which is not a valid matlab variable name, then most likely you'd access that column with
t.Mean_Value
0 件のコメント
aderajew asayhe
2019 年 5 月 15 日
To whom may it concter,
Igot the file of matlab, it's run well... but i got a question how can do diviual into weekly profiling, monthly and yearly. to do from this matlab codes.
i help please.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!