2016-12-06 72 views
-3

我有一个矩阵'UserID'中的ID列表。我想创建一个xls或csv文件,这个UserID是它的标题行。行数是:2200000,列数是11.列的标签是1996 - 2006年。我阅读此页:用特定的行和列标题创建一个xls或csv文件

https://www.mathworks.com/matlabcentral/answers/101309-how-do-i-use-xlswrite-to-add-row-and-column-labels-to-my-matlab-matrix-when-i-write-it-to-excel-in-m

但是这个代码给我的错误。虽然有时对于行数不太适用,有时也不会回答。任何人都可以介绍一个能够做到这一点的程序? (用MATLAB或甚至C#代码)

我写这篇文章的代码:

data=zeros(2200000,11); 
data_cells=num2cell(data); 
col_header={'1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006'}; 
row_header(1:2200000,1)=UserID; 
output_matrix=[{' '} col_header; row_header data_cells]; 
xlswrite('My_file.xls',output_matrix); 

,我得到这个错误:

The specified data range is invalid or too large to write to the specified file format. Try writing to an XLSX file and use Excel A1 notation for the range argument, for example, ‘A1:D4’.

+1

我们不是在这里为你写代码。告诉我们你已经尝试了什么,并解释发生了什么问题。 – RandomStranger

+0

@Bas我更新了这篇文章。 – Eli

回答

0

当您使用xlswrite你是有限的行数您的Excel version permits

The maximum size of array A depends on the associated Excel version.

在Excel 2013中,最大值为1048576,wh ich比您的2200000x11矩阵少1151424行。

你最好使用csvwrite导出您的数据,还指尖端其中:

csvwrite does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling csvwrite. To export cell arrays with mixed alphabetic and numeric data... you must use low-level export functions to write your data.

编辑:

在你的情况,你应该至少这个变化的部分代码:

col_header = 1996:2006; 
output_matrix=[0, col_header; row_header data]; 

而且您不需要定义output_matrix作为单元阵列(并且不需要data_dells)。但是,您可能还必须将UserID转换为数字变量。

+0

我写了这段代码:'data = zeros(1000000,11); data_cells = num2cell(data); col_header = {'1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006'}; row_header(1:1000000,1)= userId(1:1000000); output_matrix = [{''} col_header; row_header data_cells]; csvwrite('My_file.csv',cell2mat(output_matrix));'但是这给了我错误:'输入单元数组的所有内容必须是相同的数据类型'我不知道这个错误的原因!另一个问题:我可以将所有2200000行保存在csv文件中吗? – Eli

+0

正如上面引用的文档中所述,您只能导出一个_“仅包含数字数据的单元数组”_。你的'col_header',也可能''row_header'是字符串,并且在你的行'output_matrix = [{''} col_header; row_header data_cells]'你在字符串数组中添加一个char(''''),并且使它成为一个混合字符。 – EBH