How to update column header

2 ビュー (過去 30 日間)
Abebe kebede
Abebe kebede 2015 年 3 月 20 日
コメント済み: Image Analyst 2015 年 3 月 20 日
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.

回答 (1 件)

Image Analyst
Image Analyst 2015 年 3 月 20 日
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 件のコメント
Abebe kebede
Abebe kebede 2015 年 3 月 20 日
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 2015 年 3 月 20 日
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.

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

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by