北太天元處理含有字符串和數(shù)字兩種數(shù)據(jù)類(lèi)型的excell表格
%在北太天元讀入和寫(xiě)入含有字符串和數(shù)字混合數(shù)據(jù)類(lèi)型的excell表格
data = [ "張三", "數(shù)學(xué)家"
"張三一", "數(shù)學(xué)家"
"張三二", "數(shù)學(xué)家"
? ? ? "李四", "文學(xué)家" ?
? ? ? "王二", "經(jīng)濟(jì)學(xué)家" ];?
income = [ 100 ; 200; 300; 400; 500];
writematrix(data, '名人收入.xlsx', "FileType", "spreadsheet");?
%把收入追加在上面的excell表格中,從第C列的第一行開(kāi)始寫(xiě)入
writematrix(income, '名人收入.xlsx', "FileType", "spreadsheet", "Range", "C1");?
a = readmatrix('名人收入.xlsx',"OutputType","string");
%讀入的a都是字符串,第三列收入需要轉(zhuǎn)成double
income = str2double(a(:,3));?
%計(jì)算名人總收入
total = sum(income,1);
%獲得第二列以數(shù)學(xué)開(kāi)頭的數(shù)據(jù)行號(hào)
ind = startsWith(a(:,2), "數(shù)學(xué)");
c = a(find(ind),:);
%計(jì)算數(shù)學(xué)名人的總收入
income_數(shù)學(xué) = sum( str2double(c(:,3)), ?1);
%也可以寫(xiě)成下面一行來(lái)代替
%income_數(shù)學(xué) = sum( income(find(ind),:), ?1);
% 把數(shù)學(xué)家寫(xiě)到新的excell文件中
writematrix(c, '名人收入_數(shù)學(xué).xlsx', "FileType", "spreadsheet")?
% 把數(shù)學(xué)名人總收入增加到名人收入_數(shù)學(xué).xlsx 中
writematrix('數(shù)學(xué)名人總收入', '名人收入_數(shù)學(xué).xlsx', "FileType", "spreadsheet", 'Range', 'D1') ;
writematrix(income_數(shù)學(xué), '名人收入_數(shù)學(xué).xlsx', "FileType", "spreadsheet", 'Range', 'D2') ;