2017-04-06 75 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