声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 12095|回复: 19

[混合编程] 关于将matlab计算得到的数据输入excel中的问题。

[复制链接]
发表于 2006-10-17 10:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
经过大家的指导,我已经学会了如何将excel数据库中的数据倒入matlab中了,可是我现在想知道如何将已经算好的matlab数值放入存放原始数据的excel文件中的某个确定列中。
看到前面的帖子有讲道用加入语句”wk1write('datas',S);“。可是我运行却在excel中得不到相应的数据
请问怎么解决?
是不是还有别的方法?
非常感谢!!

[ 本帖最后由 lxq 于 2006-11-22 21:30 编辑 ]
回复
分享到:

使用道具 举报

发表于 2006-10-17 10:53 | 显示全部楼层

关于将matlab计算得到的数据输入excel中的问题

好象可以先复制为.txt文件,再粘贴到excel中.
 楼主| 发表于 2006-10-17 11:19 | 显示全部楼层
可不可以通过语句,直接将数据倒入excel中。
发表于 2006-10-17 19:37 | 显示全部楼层

如果是纯数字

那么用xlswrite,具体用法可以参考matlab 的help
还可以用工具箱Excel Link ,有问题再交流

评分

1

查看全部评分

 楼主| 发表于 2006-10-18 14:27 | 显示全部楼层
谢谢楼上,用xlswrite我已经实现了.
顺便问一下:用xlswrite不能把多项式传到excel里阿!
请问 什么语句可以使多项式倒入excel
谢谢
发表于 2006-10-18 15:48 | 显示全部楼层
楼主,能不能把如何将excel数据库中的数据倒入matlab中如何将已经算好的matlab数值放入excel文件中的某个确定列中的m文件上传上来,让大家学习一下呢?谢谢
发表于 2006-10-18 16:24 | 显示全部楼层
数据太大了不可以吧。
上次就遇到这个问题,就是因为数据太大不能写进去。也不知道高手还有什么妙招!

[ 本帖最后由 ChaChing 于 2010-4-3 01:07 编辑 ]
 楼主| 发表于 2006-10-19 10:03 | 显示全部楼层
我倒入数据用的是c=xlsread('tong.xls');
导出数据用的是xlswrite(‘文件位子和名称’,矩阵名,'excel的页码','第几行第几列');
例如:xlswrite('d:\Program Files\MATLAB71\work\tong.xls',erro_y','sheet3','C2');

这里想请问各位:怎样将一个多项式倒入excel中
用xlswrite('d:\Program Files\MATLAB71\work\tong.xls',erro_y','sheet3','C2');倒入后就会在excel中看到多项式的每一数字就占用excel一行,这样一个6项的多项式要占用很多行数,多项式看的很分散,请问个位高手有什么好的方法吗?

怎样能将几个汉字,写到excel里的一个单元格里。例如:将‘误差结果’这几个字,写到A2里。
我用了xlswrite('d:\Program Files\MATLAB71\work\tong.xls',aa,'sheet3','A2');可是得到的结果却是,A2,B2,C2,D2里 每个格里一个汉字,要怎样解决???
牛人们,帮帮忙阿!!

[ 本帖最后由 ChaChing 于 2010-4-4 19:02 编辑 ]
发表于 2006-10-19 15:00 | 显示全部楼层
和上面的问题一模一样

aa=cell(1);
aa{1}='误差结果';
xlswrite(''d:\Program Files\MATLAB71\work\tong.xls',aa,'sheet3','A2');

在帖子http://forum.vibunion.com/thread-28753-1-1.html中的4楼已经作了回复

评分

1

查看全部评分

发表于 2006-10-19 15:34 | 显示全部楼层
原帖由 ydliu_zn 于 2006-10-18 15:48 发表
楼主,能不能把如何将excel数据库中的数据倒入matlab中和如何将已经算好的matlab数值放入excel文件中的某个确定列中的m文件上传上来,让大家学习一下呢?谢谢


如果你用的是7.0或以上版本则matlab已经自带了,如果是7.0以下版本可以用下面的函数
  1. function [success,message]=xlswrite(file,data,sheet,range)
  2. % XLSWRITE Stores numeric array or cell array in Excel workbook.
  3. %   [SUCCESS,MESSAGE]=XLSWRITE(FILE,ARRAY,SHEET,RANGE) writes ARRAY to the Excel
  4. %   workbook, FILE, into the area, RANGE in the worksheet specified in SHEET.
  5. %   FILE and ARRAY must be specified. If either FILE or ARRAY is empty, a
  6. %   error is thrown and XLSWRITE terminates. The first worksheet of the
  7. %   workbook is the default. If SHEET does not exist, a new sheet is added at
  8. %   the end of the worksheet collection. If SHEET is an index larger than the
  9. %   number of worksheets, new sheets are appended until the number of worksheets
  10. %   in the workbook equals SHEET. The size defined  by the  RANGE should fit the
  11. %   size of ARRAY or contain only the first cell, e.g. 'A2'. If RANGE is larger
  12. %   than the size of ARRAY, Excel will fill the remainder of the region with
  13. %   #N/A. If RANGE is smaller than the size of ARRAY, only the sub-array that
  14. %   fits into RANGE will be written to FILE. The success of the operation is
  15. %   returned in SUCCESS and any accompanying message, in MESSAGE. On error,
  16. %   MESSAGE shall be a struct, containing the error message and message ID.
  17. %   See NOTE 1.
  18. %
  19. %   [SUCCESS,MESSAGE]=XLSWRITE(FILE,ARRAY) writes ARRAY to the Excel workbook,
  20. %   FILE, starting at cell A1 of the first worksheet. The return values are as
  21. %   for the above example.
  22. %
  23. %   XLSWRITE ARRAY FILE, is the command line version of the above example.
  24. %
  25. %   INPUT PARAMETERS:
  26. %       file:   string defining the workbook file to write to.
  27. %               Default directory is pwd; default extension 'xls'.
  28. %       array:  m x n numeric array or cell array.
  29. %       sheet:  string defining worksheet name;
  30. %               double, defining worksheet index.
  31. %       range:  string defining data region in worksheet, using the Excel
  32. %               'A1' notation.
  33. %
  34. %   RETURN PARAMETERS:
  35. %       SUCCESS: logical scalar.
  36. %       MESSAGE: struct containing message field and message_id field.
  37. %
  38. %   EXAMPLES:
  39. %
  40. %   SUCCESS = XLSWRITE('c:\matlab\work\myworkbook.xls',A,'A2:C4') will write A to
  41. %   the workbook file, myworkbook.xls, and attempt to fit the elements of A into
  42. %   the rectangular worksheet region, A2:C4. On success, SUCCESS will contain true,
  43. %   while on failure, SUCCESS will contain false.
  44. %
  45. %   NOTE 1: The above functionality depends upon Excel as a COM server. In
  46. %   absence of Excel, ARRAY shall be written as a text file in CSV format. In
  47. %   this mode, the SHEET and RANGE arguments shall be ignored.
  48. %
  49. % See also XLSREAD, WK1WRITE, CSVWRITE.
  50. %

  51. %   JP Barnard
  52. %   Copyright 1984-2005 The MathWorks, Inc.
  53. %   $Revision: 1.1.6.8 $  $Date: 2005/06/21 19:35:45 $
  54. %==============================================================================
  55. % Set default values.
  56. Sheet1 = 1;

  57. if nargin < 3
  58.     sheet = Sheet1;
  59.     range = '';
  60. elseif nargin < 4
  61.     range = '';
  62. end

  63. if nargout > 0
  64.     success = true;
  65.     message = struct('message',{''},'identifier',{''});
  66. end

  67. % Handle input.
  68. try
  69.     % handle requested Excel workbook filename.
  70.     if ~isempty(file)
  71.         if ~ischar(file)
  72.             error('MATLAB:xlswrite:InputClass','Filename must be a string');
  73.         end
  74.         % check for wildcards in filename
  75.         if any(findstr('*', file))
  76.             error('MATLAB:xlswrite:FileName', 'Filename must not contain *');
  77.         end
  78.         [Directory,file,ext]=fileparts(file);
  79.         if isempty(ext) % add default Excel extension;
  80.             ext = '.xls';
  81.         end
  82.         file = abspath(fullfile(Directory,[file ext]));
  83.         [a1 a2 a3] = fileattrib(file);
  84.         if a1 && ~(a2.UserWrite == 1)
  85.             error('MATLAB:xlswrite:FileReadOnly', 'File can not be read only.');
  86.         end
  87.     else % get workbook filename.
  88.         error('MATLAB:xlswrite:EmptyFileName','Filename is empty.');
  89.     end

  90.     % Check for empty input data
  91.     if isempty(data)
  92.         error('MATLAB:xlswrite:EmptyInput','Input array is empty.');
  93.     end

  94.     % Check for N-D array input data
  95.     if ndims(data)>2
  96.         error('MATLAB:xlswrite:InputDimension',...
  97.             'Dimension of input array cannot be higher than two.');
  98.     end

  99.     % Check class of input data
  100.     if ~(iscell(data) || isnumeric(data) || ischar(data)) && ~islogical(data)
  101.         error('MATLAB:xlswrite:InputClass',...
  102.             'Input data must be a numeric, cell, or logical array.');
  103.     end

  104.     % convert input to cell array of data.
  105.     if iscell(data)
  106.         A=data;
  107.     else
  108.         A=num2cell(data);
  109.     end

  110.     if nargin > 2
  111.         % Verify class of sheet parameter.
  112.         if ~(ischar(sheet) || (isnumeric(sheet) && sheet > 0))
  113.             error('MATLAB:xlswrite:InputClass',...
  114.                 'Sheet argument must a string or a whole number greater than 0.');
  115.         end
  116.         if isempty(sheet)
  117.             sheet = Sheet1;
  118.         end
  119.         % parse REGION into sheet and range.
  120.         % Parse sheet and range strings.
  121.         if ischar(sheet) && ~isempty(strfind(sheet,':'))
  122.             range = sheet; % only range was specified.
  123.             sheet = Sheet1;% Use default sheet.
  124.         elseif ~ischar(range)
  125.             error('MATLAB:xlswrite:InputClass',...
  126.                 'Range argument must a string of Excel A1 notation.');
  127.         end
  128.     end

  129. catch
  130.     if ~isempty(nargchk(2,4,nargin))
  131.         error('MATLAB:xlswrite:InputArguments',nargchk(2,4,nargin));
  132.     elseif nargout == 0
  133.         rethrow(lasterror);     % Display last error.
  134.     else
  135.         success = false;
  136.         message = lasterror;       % Return last error.
  137.     end
  138.     return;
  139. end
  140. %------------------------------------------------------------------------------
  141. % Attempt to start Excel as ActiveX server.
  142. try
  143.     Excel = actxserver('Excel.Application');

  144. catch
  145.     warning('MATLAB:xlswrite:NoCOMServer',...
  146.         ['Could not start Excel server for export.\n' ...
  147.         'XLSWRITE attempts to file in CSV format.']);
  148.     if nargout > 0
  149.         [message.message,message.identifier] = lastwarn;
  150.     end
  151.     % write data as CSV file, that is, comma delimited.
  152.     file = regexprep(file,'(\.xls)$','.csv'); % change extention to 'csv'.
  153.     try
  154.         dlmwrite(file,data,','); % write data.
  155.     catch
  156.         [last_error] = lasterror;
  157.         erroridentifier = 'MATLAB:xlswrite:dlmwrite';
  158.         errormessage = ['An error occurred on data export in CSV format.\n',...
  159.             last_error.message];
  160.         if nargout == 0
  161.             % Throw error.
  162.             error(erroridentifier,errormessage);
  163.         else
  164.             success = false;
  165.             message.message = sprintf(errormessage);
  166.             message.identifier = erroridentifier;
  167.         end
  168.     end
  169.     return;
  170. end
  171. %------------------------------------------------------------------------------
  172. try
  173.     % Construct range string
  174.     if isempty(strfind(range,':'))
  175.         % Range was partly specified or not at all. Calculate range.
  176.         [m,n] = size(A);
  177.         range = calcrange(range,m,n);
  178.     end
  179. catch
  180.     if nargout == 0
  181.         rethrow(lasterror);     % Display last error.
  182.     else
  183.         success = false;
  184.         message = lasterror;       % Return last error.
  185.     end
  186.     return;
  187. end

  188. %------------------------------------------------------------------------------
  189. try
  190.     if ~exist(file,'file')
  191.         % Create new workbook.  
  192.         
  193.         %This is in place because in the presence of a Google Desktop
  194.         %Search installation, calling Add, and then SaveAs after adding data,
  195.         %to create a new Excel file, will leave an Excel process hanging.  
  196.         %This workaround prevents it from happening, by creating a blank file,
  197.         %and saving it.  It can then be opened with Open.
  198.         ExcelWorkbook = Excel.workbooks.Add;
  199.         ExcelWorkbook.SaveAs(file,1);
  200.         ExcelWorkbook.Close(false);
  201.     end
  202.    
  203.     %Open file
  204.     ExcelWorkbook = Excel.workbooks.Open(file);
  205.    
  206.     try % select region.
  207.         % Activate indicated worksheet.
  208.         message = activate_sheet(Excel,sheet);

  209.         % Select range in worksheet.
  210.         Select(Range(Excel,sprintf('%s',range)));

  211.     catch % Throw data range error.
  212.         error('MATLAB:xlswrite:SelectDataRange',lasterr);
  213.     end

  214.     % Export data to selected region.
  215.     set(Excel.selection,'Value',A);
  216.     ExcelWorkbook.Save
  217.     ExcelWorkbook.Close(false)  % Close Excel workbook.
  218.     Excel.Quit;
  219. catch
  220.     try
  221.         ExcelWorkbook.Close(false);    % Close Excel workbook.
  222.     end
  223.     Excel.Quit;
  224.     delete(Excel);                 % Terminate Excel server.
  225.     if nargout == 0
  226.         rethrow(lasterror);     % Display last error.
  227.     else
  228.         success = false;
  229.         message = lasterror;       % Return last error.
  230.     end
  231. end
  232. %--------------------------------------------------------------------------
  233. function message = activate_sheet(Excel,Sheet)
  234. % Activate specified worksheet in workbook.

  235. % Initialise worksheet object
  236. WorkSheets = Excel.sheets;
  237. message = struct('message',{''},'identifier',{''});

  238. % Get name of specified worksheet from workbook
  239. try
  240.     TargetSheet = get(WorkSheets,'item',Sheet);
  241. catch
  242.     % Worksheet does not exist. Add worksheet.
  243.     TargetSheet = addsheet(WorkSheets,Sheet);
  244.     warning('MATLAB:xlswrite:AddSheet','Added specified worksheet.');
  245.     if nargout > 0
  246.         [message.message,message.identifier] = lastwarn;
  247.     end
  248. end

  249. % activate worksheet
  250. Activate(TargetSheet);
  251. %------------------------------------------------------------------------------
  252. function newsheet = addsheet(WorkSheets,Sheet)
  253. % Add new worksheet, Sheet into worsheet collection, WorkSheets.

  254. if isnumeric(Sheet)
  255.     % iteratively add worksheet by index until number of sheets == Sheet.
  256.     while WorkSheets.Count < Sheet
  257.         % find last sheet in worksheet collection
  258.         lastsheet = WorkSheets.Item(WorkSheets.Count);
  259.         newsheet = WorkSheets.Add([],lastsheet);
  260.     end
  261. else
  262.     % add worksheet by name.
  263.     % find last sheet in worksheet collection
  264.     lastsheet = WorkSheets.Item(WorkSheets.Count);
  265.     newsheet = WorkSheets.Add([],lastsheet);
  266. end
  267. % If Sheet is a string, rename new sheet to this string.
  268. if ischar(Sheet)
  269.     set(newsheet,'Name',Sheet);
  270. end

  271. %------------------------------------------------------------------------------
  272. function [absolutepath]=abspath(partialpath)

  273. % parse partial path into path parts
  274. [pathname filename ext] = fileparts(partialpath);
  275. % no path qualification is present in partial path; assume parent is pwd, except
  276. % when path string starts with '~' or is identical to '~'.
  277. if isempty(pathname) && isempty(strmatch('~',partialpath))
  278.     Directory = pwd;
  279. elseif isempty(regexp(partialpath,'(.:|\\\\)')) && ...
  280.         isempty(strmatch('/',partialpath)) && ...
  281.         isempty(strmatch('~',partialpath));
  282.     % path did not start with any of drive name, UNC path or '~'.
  283.     Directory = [pwd,filesep,pathname];
  284. else
  285.     % path content present in partial path; assume relative to current directory,
  286.     % or absolute.
  287.     Directory = pathname;
  288. end

  289. % construct absulute filename
  290. absolutepath = fullfile(Directory,[filename,ext]);
  291. %------------------------------------------------------------------------------
  292. function range = calcrange(range,m,n)
  293. % Calculate full target range, in Excel A1 notation, to include array of size
  294. % m x n

  295. range = upper(range);
  296. cols = isletter(range);
  297. rows = ~cols;
  298. % Construct first row.
  299. if ~any(rows)
  300.     firstrow = 1; % Default row.
  301. else
  302.     firstrow = str2double(range(rows)); % from range input.
  303. end
  304. % Construct first column.
  305. if ~any(cols)
  306.     firstcol = 'A'; % Default column.
  307. else
  308.     firstcol = range(cols); % from range input.
  309. end
  310. try
  311.     lastrow = num2str(firstrow+m-1);   % Construct last row as a string.
  312.     firstrow = num2str(firstrow);      % Convert first row to string image.
  313.     lastcol = dec2base27(base27dec(firstcol)+n-1); % Construct last column.

  314.     range = [firstcol firstrow ':' lastcol lastrow]; % Final range string.
  315. catch
  316.     error('MATLAB:xlswrite:CalculateRange',...
  317.         'Data range must be between A1 and IV65536.');
  318. end

  319. %------------------------------------------------------------------------------
  320. function s = dec2base27(d)

  321. %   DEC2BASE27(D) returns the representation of D as a string in base 27,
  322. %   expressed as 'A'..'Z', 'AA','AB'...'AZ', until 'IV'. Note, there is no zero
  323. %   digit, so strictly we have hybrid base26, base27 number system.  D must be a
  324. %   negative integer bigger than 0 and smaller than 2^52, which is the maximum
  325. %   number of columns in an Excel worksheet.
  326. %
  327. %   Examples
  328. %       dec2base(1) returns 'A'
  329. %       dec2base(26) returns 'Z'
  330. %       dec2base(27) returns 'AA'
  331. %-----------------------------------------------------------------------------
  332. b = 26;
  333. symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

  334. d = d(:);
  335. if d ~= floor(d) | any(d < 0) | any(d > 1/eps)
  336.     error('MATLAB:xlswrite:Dec2BaseInput',...
  337.         'D must be an integer, 0 <= D <= 2^52.');
  338. end

  339. % find the number of columns in new base
  340. n = max(1,round(log2(max(d)+1)/log2(b)));
  341. while any(b.^n <= d)
  342.     n = n + 1;
  343. end

  344. % set b^0 column
  345. s(:,n) = rem(d,b);
  346. while n > 1 && any(d)
  347.     if s(:,n) == 0
  348.         s(:,n) = b;
  349.     end
  350.     if d > b
  351.         % after the carry-over to the b^(n+1) column
  352.         if s(:,n) == b
  353.             % for the b^n digit at b, set b^(n+1) digit to b
  354.             s(:,n-1) = floor(d/b)-1;
  355.         else
  356.             % set the b^(n+1) digit to the new value after the last carry-over.
  357.             s(:,n-1) = rem(floor(d/b),b);
  358.         end
  359.     else
  360.         s(:,n-1) = []; % remove b^(n+1) digit.
  361.     end
  362.     n = n - 1;
  363. end
  364. s = symbols(s);
  365. %------------------------------------------------------------------------------
  366. function d = base27dec(s)
  367. %   BASE27DEC(S) returns the decimal of string S which represents a number in
  368. %   base 27, expressed as 'A'..'Z', 'AA','AB'...'AZ', until 'IV'. Note, there is
  369. %   no zero so strictly we have hybrid base26, base27 number system.
  370. %
  371. %   Examples
  372. %       base27dec('A') returns 1
  373. %       base27dec('Z') returns 26
  374. %       base27dec('IV') returns 256
  375. %-----------------------------------------------------------------------------

  376. d = 0;
  377. b = 26;
  378. n = numel(s);
  379. for i = n:-1:1
  380.     d = d+(s(i)-'A'+1)*(b.^(n-i));
  381. end
  382. %-------------------------------------------------------------------------------
复制代码

评分

1

查看全部评分

发表于 2006-10-19 15:35 | 显示全部楼层
原帖由 ppdd 于 2006-10-18 16:24 发表
上次就遇到这个问题,就是因为数据太大不能写进去。也不知道高手还有什么妙招!



没试过,不过应该可以通过分批写入的方式完成
发表于 2006-10-20 09:16 | 显示全部楼层
happy教授真是个高手啊,谢谢
发表于 2006-10-21 21:37 | 显示全部楼层
使用通过,谢谢HAPPY.
发表于 2006-11-9 10:05 | 显示全部楼层
我想问一下上面的高手,excel表里的字符变量怎么读入matlab里面呢,我在用matlab的统计工具,用决策树对数据分类,生成树。可是总是提示我变量错误,我也不知道怎么回事,请指教,谢谢。
发表于 2006-11-9 14:22 | 显示全部楼层
原帖由 lpwy2005 于 2006-10-21 21:37 发表
使用通过,谢谢HAPPY.


我用的是6.0.烤入这个xlswrite这个函数之后,有很多不通的地方,不知是怎么回事
88的&&~这个不认,还有107的||两竖也不认
?? Error: File: F:\matlab\work\xlswrite.m Line: 107 Column: 23
")" expected, "identifier" found.
是怎么回事呢
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-20 23:49 , Processed in 0.083879 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表