How to update column header

2 views (last 30 days)
Abebe kebede
Abebe kebede on 20 Mar 2015
Commented: Image Analyst on 20 Mar 2015
I Have cell array data (1000 by 3)which contail information about people based on their gender
Number of F
Number of M
M [] []
F [] []
F [] []
M [] []
F [] []
M [] []
M [] []
. . .
. . .
Only column two has a header. row 1 is number of F and row 2 is number of M. Since the number of people changes from time to time, how can I make the header count the number of F and M and update it self.

Answers (1)

Image Analyst
Image Analyst on 20 Mar 2015
You didn't give any data so I'm just giving untested code, but you can't have it update itself - you have to do it manually. So you need to sum the column and rewrite the header cells whenever it needs to be updates. So you need to do something like this
columns = data{3:end, 2:3}; % Extract numerical array
sexes = data{3:end, 1};
maleIndexes = sexes == 'M';
femaleIndexes= sexes == 'F';
sumMales = sum(column(maleIndexes,:));
sumFemales = sum(column(femaleIndexes,:));
% Update headers
data{1,2} = sprintf('Number of Males = %d', sumMales(1));
data{1,3} = sprintf('Number of Males = %d', sumMales(2));
data{2,2} = sprintf('Number of Females = %d', sumFemales(1));
data{2,3} = sprintf('Number of Females = %d', sumFemales(2));
You can put that all into a function called UpdateHeaders and call it.
function data = UpdateHeaders(data)
try
columns = data{3:end, 2:3}; % Extract numerical array
sexes = data{3:end, 1};
maleIndexes = sexes == 'M';
femaleIndexes= sexes == 'F';
sumMales = sum(column(maleIndexes,:));
sumFemales = sum(column(femaleIndexes,:));
% Update headers
data{1,2} = sprintf('Number of Males = %d', sumMales(1));
data{1,3} = sprintf('Number of Males = %d', sumMales(2));
data{2,2} = sprintf('Number of Females = %d', sumFemales(1));
data{2,3} = sprintf('Number of Females = %d', sumFemales(2));
% Some code that might generate an error.
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
Then call it in your main code or callback:
data = UpdateHeaders(data);
  2 Comments
Abebe kebede
Abebe kebede on 20 Mar 2015
hanks but there is an error when i try to excute,
maleIndexes = sexes == 'M'; ----- undefined function 'eq' for inpute arguments of type 'cell.
Image Analyst
Image Analyst on 20 Mar 2015
Then use ismember() or strcmpi() instead. If neither of those work, then it's time for you to supply code to generate the data so that people can help you with your actual data . Also supply your m-code for ismember or strcmpi.

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!