Matlab剪贴板精度 - 格式长 -

时间:2015-04-24 11:24:22

标签: matlab

我需要将几个矩阵从matlab复制到excel,所以我做了我的研究,我发现了一个名为num2clip的非常棒的脚本,它将所选的数组带到了剪贴板。 唯一的问题是数字格式很短,当我希望它很长时。 我怀疑"双"在脚本中使用的类型,但我仍然是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); 

祝你好运。

2 个答案:

答案 0 :(得分:0)

只需将该行替换为:

arraystring = num2str(array) ; 

到这样的一行:

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

这将为您提供double类型(15位数)所能达到的最高精度。

查看num2str文档了解更多自定义格式。

答案 1 :(得分:0)

感谢Hoki的参与。 我没有时间浏览所有文档,顺便说一下。 当我尝试你的解决方案时,复制的数据都插在一个单元格上,我只需要改变:      arraystring = num2str(array,'%15.15f'); 至      arraystring = num2str(array,15);

祝你有愉快的一天!