2013-03-21 209 views
0

我正在努力解决一个问题,并希望获得简单的解决方法。我有一大堆数据,我有一些向量值(1.02 1.23 3.32)格式。我想以表格形式将它作为1.02 1.23 3.32。这里的问题是有两种类型的分隔符'('和')'。Matlab在导入过程中删除多个分隔符

任何人都可以帮忙为此编写代码吗?我有这样的事情:

filename = 'U.dat'; 
delimiterIn = '('; 
headerlinesIn = 0; 
A = textscan(filename,delimiterIn,headerlinesIn); 

但有一件事是,它只能有一个分隔符“(”和它也不管用

NISHANT

+1

更长的数据样本和有关文件整体格式的更具体信息将有助于确定此问题的最佳答案。 – wakjah 2013-03-21 14:34:30

回答

0

如果您的文本文件看起来是这样的:

(1 2 3) (4 5 6) 
(7 8 9) (10 11 12) 

你可以把它读作字符串并将其转换为向量单元阵列,像这样:

% read in file 
clear all 
filename = 'delim.txt'; 
fid = fopen(filename); %opens file for reading 
tline = fgets(fid); %reads in a line 
index = 1; 
%reads in all lines until the end of the file is reached 
while ischar(tline) 
    data{index} = tline;  
    tline = fgets(fid);   
    index = index + 1; 
end 
fclose(fid); %close file 

% convert strings to a cell array of vectors 
rowIndex = 1; 
colIndex = 1; 
outData = {}; 
innerStr = []; 

for aCell = data % for each entry in data 
    sline = aCell{1,1}; 
    for c = sline % for each charecter in the line 
     if strcmp(c, '(') 
      innerStr = []; 
     elseif strcmp(c, ')') 
      outData{rowIndex,colIndex} = num2str(innerStr); 
      colIndex = colIndex + 1; 
     else 
      innerStr = [innerStr, c]; 
     end 
    end 

    rowIndex = rowIndex + 1; 
    colIndex = 1; 
end 

outData