How to Iterate over the columns of a Matlab table?

169 ビュー (過去 30 日間)
Ciaran
Ciaran 2015 年 10 月 25 日
コメント済み: Image Analyst 2020 年 9 月 8 日
How can I iterate over the columns of a Matlab table? The Python equivalent and assuming data was a pandas data frame would be:
variable_names=data.keys() #get variable names
for i in variable_names: # iterate over and print all data using variable names
print data[i]
But the corresponding for a Matlab table does not work:
f='Vilar_data.xls';
data=readtable(f);
variables=data.Properties.VariableNames; %get variable names
for i=variables,
data(1:end,{i}) %attemt to iterate over the data by column headings
end
  1 件のコメント
Oleg Komarov
Oleg Komarov 2015 年 10 月 25 日
You need to remove the brackets from {i}, because i will already be a cell containing a string.

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

採用された回答

Geoff Hayes
Geoff Hayes 2015 年 10 月 25 日
編集済み: Geoff Hayes 2015 年 10 月 25 日
Ciaran - access data in a table illustrates a couple of different ways in which you can access the column data within the table. You can access the column either by its variable index or by its variable name. For example, if you wish to access each column by the variable index, you could something like the following
for k=1:length(variables)
colData = data.(k);
end
In the above, we iterate over each variable and access the column data directly through the variable index.
  3 件のコメント
Maryam Abbaszadeh
Maryam Abbaszadeh 2020 年 9 月 8 日
x = [];
x = ['A':'Z'];
for i='A':'Z'
for j='A':'Z'
x = [cellstr(x),[i,j]];
end
end
for i=1:100
app.UITable.ColumnName ={x(i)};
end
i want to change ColumnNames with x characters but it doesn't work with above code
how can I solve this problem?
Image Analyst
Image Analyst 2020 年 9 月 8 日
No idea what you want. Please read this link then post your question in a new question along with your desired output.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2015 年 10 月 25 日
I would not iterate over variable names, unless they might possibly be created by some weird program that changes what column the data were in. I'd simply just use column number - it's so so much simpler. Run this demo:
% Create sample data (from the help documentation).
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
% Create a table, T, as a container for the workspace variables.
T = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)
% Let's see what methods T has
methods(T)
% Now let's iterate over all columns using column number.
for col = 1 : width(T)
fprintf('\nHere is column #%d, which is called %s\n', col, T.Properties.VariableNames{col})
thisColumn = T(:, col) % Extract this one column into its own variable.
% Now do whatever you want with "thisColumn" variable.
end
  2 件のコメント
Ciaran
Ciaran 2015 年 10 月 25 日
編集済み: Ciaran 2015 年 10 月 25 日
Hi Image Analyst, yes, precisely. I'm trying to write a program that will take any table so I think iterating over the column headings is best. Thank you though, your answer is still very informative
Image Analyst
Image Analyst 2015 年 10 月 26 日
I don't think so. Plus it's way more complicated. Why do you think it's better? My code will work with any table just using a simple index like you're already used to with other variable types. I'm not sure why you imply my code won't "take any table." It certainly will, and in a lot more straightforward manner.

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


Walter Roberson
Walter Roberson 2015 年 10 月 25 日
for i=variables
data(1:end,i{1})
end

カテゴリ

Help Center および File ExchangeOther Formats についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by