2015-04-24 51 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);

祝您有美好的一天!