我想问一下如何使用MATLAB将新列添加到现有的excel文件中而不更改文件中的原始数据?在我的例子中,我不知道文件中的原始列数和行数,而且在实践中逐个打开文件并检查效率很低。另一个困难是,新的列可能与现有数据的行数不同,因此我无法使用读取数据的技巧,形成新的矩阵并用新的矩阵替换数据。
我见过许多教人们如何添加新行的帖子,但添加新列似乎是一件完全不同的事情,因为列是用字母而不是数字命名的。
谢谢。
发布于 2016-06-21 11:26:10
您可以尝试读取数据,在数组上使用size来确定列数,然后对所需的范围使用xlswrite。这里有一个将列号转换为excel格式的函数:http://au.mathworks.com/matlabcentral/answers/54153-dynamic-ranges-using-xlswrite
发布于 2016-06-21 12:31:55
最后,我用下面的代码解决了这个问题:
%%%
if (step==1)
xlswrite(filename,array,sheetname,'A1'); %Create the file
else
[~,~,Data]=xlsread(filename,sheetname); %read in all the old data
OriCol=size(Data,2); %get the column number of the old data
NewCol=OriCol+1; %the new array is placed right next to the original data
ColLetter=xlcolumnletter(NewCol);
StartCell=[ColLetter,'1'];
xlswrite(filename,array,sheetname,StartCell);
endhttps://stackoverflow.com/questions/37934883
复制相似问题