【问题标题】:how to Save Data in Excel Sheet using Matlab?如何使用 Matlab 在 Excel 表中保存数据?
【发布时间】:2014-08-14 18:08:23
【问题描述】:

我想将我的数据以表格的形式保存在 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

【问题讨论】:

  • 我已经更新了我的答案,使其更加详细。

标签: excel matlab


【解决方案1】:

使用 @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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      将表格设为单元矩阵,然后使用 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. 
      

      这样更有帮助吗?

      【讨论】:

      • 我是 Matlab 的菜鸟
      • 第一个工作正常,但在第二个“xlswrite('file',output);”不正确应该是 xlswrite(file,output);并感谢您的帮助
      • 是否必须阅读过往记录?如果记录超过1000条怎么办,不是很耗时吗?
      • 对于 xls 文件,您必须打开它并重新编写。如果您能够使用 *.csv 格式,您可以更快地在文件中追加一行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      相关资源
      最近更新 更多