如何保存数据在Excel表中使用Matlab?

我想以Excel表格的forms保存我的数据。 它应该看起来像:

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

variablesn ='john',ag = 22,rn = 44,gp = 3.53

使用@Tom给出的代码

如果它不存在,它将创build一个新文件,如果它存在,那么它将追加下面的行。

 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 

您可以使用xlswrite()函数如下所示:

 filename = 'Data.xlsx'; Data = {'john', 22, 44, 3.53}; xlswrite(filename,Data); 

获取更多信息,您可以阅读xlswrite()函数的帮助。

将你的表格作为单元格matrix,然后使用xlswrite将其保存为xls

这将允许您导出具有混合内容types(数字和文本)的表格。

 [~,~,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. 

这更有帮助吗?