2017-04-06 73 views
0

打开一个格式化的文本文件,我需要打开MATLAB一个文本文件,与此类似:如何在MATLAB

(1.234,2.345,3.456) 
(1.111,2.222,3.333) 
(5.432,4.321,5.432) 

我试过dlmreadfscanfimportdata但他们都不来工作。我试图将这个文件读入一个3x3数组,我不知道该怎么做。

+3

你能告诉我们,你实际上试过'fscanf'代码? – Suever

+1

对于这个问题,展示你所有的尝试,并详细解释每个人的问题。 –

回答

0

注:更新的数据字段由新行

这里有一个不雅的解决方案分开,假设文本文件被称为当前目录test.txt

% Read the text file into a string: 
    txt = fileread('test.txt'); 
% Split the string at white space: 
    A = strsplit(txt,'\n'); 
% Remove brackets: 
    B = cellfun(@(x) x(2:end-1),A,'uniformoutput',0); 
% Split groups by commas: 
    C = cellfun(@(x) strsplit(x,','),B,'uniformoutput',0); 
% Convert entries from string to double: 
    D = cellfun(@(x) cellfun(@str2num,x),C','uniformoutput',0); 
% Convert from cell to one big matrix: 
    NumArray = cell2mat(D); 

输出是

NumArray = 

    1.2340 2.3450 3.4560 
    1.1110 2.2220 3.3330 
    5.4320 4.3210 5.4320