2015-04-24 34 views
0

我需要从matlab复制粘贴几个矩阵到excel,所以我做了我的研究,我发现了一个非常了不起的脚本,名为num2clip,它将选定的数组带到剪贴板。 唯一的问题是,数字格式很短,我希望它很长。 我怀疑在脚本中使用的“双”类型,但我仍然是新来的matlab,所以我有一些重要的缺乏。Matlab剪贴板精度 - 格式长 -

这是我找到的脚本,为了保持长输入格式,我必须根据你的要求做些什么?

function arraystring = num2clip(array) 

function arraystring = num2clip(array) 

%NUM2CLIP copies a numerical-array to the clipboard 
% 
% ARRAYSTRING = NUM2CLIP(ARRAY) 
% 
% Copies the numerical array ARRAY to the clipboard as a tab-separated 
% string. This format is suitable for direct pasting to Excel and other 
% programs. 
% 
% The tab-separated result is returned as ARRAYSTRING. This 
% functionality has been included for completeness. 
% 
%Author: Grigor Browning 
%Last update: 02-Sept-2005 

%convert the numerical array to a string array 
%note that num2str pads the output array with space characters to account 
%for differing numbers of digits in each index entry 
arraystring = num2str(array); 

%add a carrige return to the end of each row 
arraystring(:,end+1) = char(10); 

%reshape the array to a single line 
%note that the reshape function reshape is column based so to reshape by 
%rows one must use the inverse of the matrix 
%reshape the array to a single line 
arraystring = reshape(arraystring',1,prod(size(arraystring))); 

%create a copy of arraystring shifted right by one space character 
arraystringshift = [' ',arraystring]; 

%add a space to the end of arraystring to make it the same length as 
%arraystringshift 
arraystring = [arraystring,' ']; 

%now remove the additional space charaters - keeping a single space 
%charater after each 'numerical' entry 
arraystring = arraystring((double(arraystring)~=32 |... 
      double(arraystringshift)~=32) &... 
      ~(double(arraystringshift==10) &... 
      double(arraystring)==32)); 

%convert the space characters to tab characters 
arraystring(double(arraystring)==32) = char(9); 
format long e 

%copy the result to the clipboard ready for pasting 
clipboard('copy',arraystring); 

此致敬礼。

+0

所以使用matlab函数xlswrite在你的情况下不起作用? – bilaly

回答

0

只需更换与线:

arraystring = num2str(array) ; 

像一条线:

arraystring = num2str(array,'%15.15f') ; 

这会给你,你可以用double类型达到最大精度(15位)。

请参阅num2str文档以获取更多自定义格式。

0

谢谢Hoki的参与。 我没有时间阅读所有的文件,这很棒。 当我尝试你的解决方案时,复制的数据全部插入到一个单元格中,我只需要更改: arraystring = num2str(array,'%15.15f'); 至 arraystring = num2str(array,15);

祝您有美好的一天!