Reputation: 7681
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
Upvotes: 0
Views: 1212
Reputation: 368
This code should do the trick
% generate some random table with 100 rows
entry = table(rand(100,1),randi(100,100,1),cellstr(char(randi([65,90],100,5))));
% extract the var names
var = entry.Properties.VariableNames;
% now for each column display the contents in three different ways
% obviously you could do whatever you like with the data
for ii=var
disp(entry.(ii{1})(1:4));% display only the first 4 entries
disp(entry.(ii{1})(:)');% display all the entries as a row
disp(entry.(ii{1})(end-4:end));% display only the last 4 entries
end
Enjoy
Upvotes: 2