我想在Excel表格中以表格的形式保存我的数据。它应该看起来像:
Name | Age | R_no | Gpa
Adnan | 24 | 18 | 3.55
Ahmad | 22 | 12 | 3.44
Usman | 23 | 22 | 3.00
每当我执行我的文件 classData.m 时,下面会添加一行。就像我想将下一行添加为
john | 21 | 44 | 3.53
变量n ='john',ag = 22,rn = 44,gp = 3.53
答案 0 :(得分:2)
使用 @Tom
提供的代码如果它不存在,它将创建一个新文件,如果它确实存在,那么它将追加下面的行。
filename='Features.xlsx';
N='Adnan'; a=22; roll=22; gpa=3.55;
fileExist = exist(filename,'file');
if fileExist==0
header = {'Name', 'age ','roll' , 'gpa'};
xlswrite(filename,header);
else
[~,~,input] = xlsread(filename); % Read in your xls file to a cell array (input)
new_data = {N, a,roll , gpa}; % This is a cell array of the new line you want to add
output = cat(1,input,new_data); % Concatinate your new data to the bottom of input
xlswrite(filename,output); % Write to the new excel file.
end
答案 1 :(得分:1)
将您的表格作为单元格矩阵,然后使用xlswrite将其保存为xls
这将允许您导出具有混合内容类型(数字和文本)的表。
[~,~,input] = xlsread('your_file.xls') % Read in your xls file to a cell array (input)
new_data = {'john', 22, 44, 3.53} % This is a cell array of the new line you want to add
output = cat(1,input,new_data); % Concatinate your new data to the bottom of input
xlswrite('output_file_name.xls',output); % Write to the new excel file.
如果你想保留相同的文件名,那么你可以稍微改变它,就像这样
file = 'file_to_update.xls';
[~,~,input] = xlsread(file) % Read in your xls file to a cell array (input)
new_data = {'john', 22, 44, 3.53} % This is a cell array of the new line you want to add
output = cat(1,input,new_data); % Concatinate your new data to the bottom of input
xlswrite('file',output); % Write to the new excel file.
这更有帮助吗?
答案 2 :(得分:1)
您可以使用xlswrite()
功能,如下所示:
filename = 'Data.xlsx';
Data = {'john', 22, 44, 3.53};
xlswrite(filename,Data);
您可以阅读有关xlswrite()
功能的更多信息。