2013-05-13 22 views
1

我需要編寫包含像標籤單元陣列:寫入特定的細胞在CSV文件

已啓動:截至:開始:截至:開始:結束:...

到特定的行和和在csv文件列,但是當我使用類似:

csvwrite(fName, tag(3), 4,0) 

它在一個單獨的細胞寫入每個字母並覆蓋現有數據(標籤(3)的類型是細胞中的載體)。

任何幫助,將不勝感激。

這是代碼的簡略版:

data = importdata('samplelog', ' '); 


for i=1:length(data) 
    % Extracting the tags 
    [start_i, end_i] = regexp(data{i}, '\D+:'); 
    tag{i} = data{i}(start_i:end_i); 
end 

fName = 'mem.csv'; 
fid = fopen(fName, 'w+'); 

csvwrite(fName, tag(3), 4, 0); 

其中samplelog是一樣的東西:

START: 2013-05-04 19:13:06.188 
ENDED: 2013-05-04 19:22:41.617 
START: 2013-05-04 19:25:52.382 
ENDED: 2013-05-04 19:35:27.827 
. 
. 
. 
+1

你試過'標記{3}'? – Shai 2013-05-13 14:17:13

+0

是的,結果是一樣的! – Gholi 2013-05-13 14:40:48

+0

你可以添加更多的代碼,以便我們可以直接運行你的代碼? – 2013-05-13 15:02:02

回答

0

隨着cell2csv.m,你只需要調用cell2csv('mem.csv', tag),它會寫入單元格將數組tag轉換成逗號分隔值文本文件mem.csv

cell2csv.m

function cell2csv(fileName, cellArray, separator, excelYear, decimal) 
% Writes cell array content into a *.csv file. 
% 
% CELL2CSV(fileName, cellArray, separator, excelYear, decimal) 
% 
% fileName  = Name of the file to save. [ i.e. 'text.csv' ] 
% cellArray = Name of the Cell Array where the data is in 
% separator = sign separating the values (default = ';') 
% excelYear = depending on the Excel version, the cells are put into 
%    quotes before they are written to the file. The separator 
%    is set to semicolon (;) 
% decimal  = defines the decimal separator (default = '.') 
% 
%   by Sylvain Fiedler, KA, 2004 
% updated by Sylvain Fiedler, Metz, 06 
% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler 
% added the choice of decimal separator, 11/2010, S.Fiedler 

%% Checking für optional Variables 
if ~exist('separator', 'var') 
    separator = ','; 
end 

if ~exist('excelYear', 'var') 
    excelYear = 1997; 
end 

if ~exist('decimal', 'var') 
    decimal = '.'; 
end 

%% Setting separator for newer excelYears 
if excelYear > 2000 
    separator = ';'; 
end 

%% Write file 
datei = fopen(fileName, 'w'); 

for z=1:size(cellArray, 1) 
    for s=1:size(cellArray, 2) 

     var = eval(['cellArray{z,s}']); 
     % If zero, then empty cell 
     if size(var, 1) == 0 
      var = ''; 
     end 
     % If numeric -> String 
     if isnumeric(var) 
      var = num2str(var); 
      % Conversion of decimal separator (4 Europe & South America) 
      % http://commons.wikimedia.org/wiki/File:DecimalSeparator.svg 
      if decimal ~= '.' 
       var = strrep(var, '.', decimal); 
      end 
     end 
     % If logical -> 'true' or 'false' 
     if islogical(var) 
      if var == 1 
       var = 'TRUE'; 
      else 
       var = 'FALSE'; 
      end 
     end 
     % If newer version of Excel -> Quotes 4 Strings 
     if excelYear > 2000 
      var = ['"' var '"']; 
     end 

     % OUTPUT value 
     fprintf(datei, '%s', var); 

     % OUTPUT separator 
     if s ~= size(cellArray, 2) 
      fprintf(datei, separator); 
     end 
    end 
    if z ~= size(cellArray, 1) % prevent a empty line at EOF 
     % OUTPUT newline 
     fprintf(datei, '\n'); 
    end 
end 
% Closing file 
fclose(datei); 
% END 

我必須在這裏繁殖許可證,不好意思:

Copyright (c) 2004-2010, Sylvain Fiedler 
All rights reserved. 

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are 
met: 

    * Redistributions of source code must retain the above copyright 
     notice, this list of conditions and the following disclaimer. 
    * Redistributions in binary form must reproduce the above copyright 
     notice, this list of conditions and the following disclaimer in 
     the documentation and/or other materials provided with the distribution 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.