欢迎光临散文网 会员登陆 & 注册

北太天元处理含有字符串和数字两种数据类型的excell表格

2022-12-08 07:37 作者:卢朓  | 我要投稿

%在北太天元读入和写入含有字符串和数字混合数据类型的excell表格

data = [ "张三", "数学家"

"张三一", "数学家"

"张三二", "数学家"

      "李四", "文学家"  

      "王二", "经济学家" ]; 

income = [ 100 ; 200; 300; 400; 500];

writematrix(data, '名人收入.xlsx', "FileType", "spreadsheet"); 

%把收入追加在上面的excell表格中,从第C列的第一行开始写入

writematrix(income, '名人收入.xlsx', "FileType", "spreadsheet", "Range", "C1"); 

a = readmatrix('名人收入.xlsx',"OutputType","string");

%读入的a都是字符串,第三列收入需要转成double

income = str2double(a(:,3)); 

%计算名人总收入

total = sum(income,1);

%获得第二列以数学开头的数据行号

ind = startsWith(a(:,2), "数学");

c = a(find(ind),:);

%计算数学名人的总收入

income_数学 = sum( str2double(c(:,3)),  1);

%也可以写成下面一行来代替

%income_数学 = sum( income(find(ind),:),  1);

% 把数学家写到新的excell文件中

writematrix(c, '名人收入_数学.xlsx', "FileType", "spreadsheet") 

% 把数学名人总收入增加到名人收入_数学.xlsx 中

writematrix('数学名人总收入', '名人收入_数学.xlsx', "FileType", "spreadsheet", 'Range', 'D1') ;

writematrix(income_数学, '名人收入_数学.xlsx', "FileType", "spreadsheet", 'Range', 'D2') ;


北太天元处理含有字符串和数字两种数据类型的excell表格的评论 (共 条)

分享到微博请遵守国家法律