如何在MATLAB中打开格式化文本文件
我需要在matlab中打开一个类似以下内容的文本文件:如何在MATLAB中打开格式化文本文件,matlab,Matlab,我需要在matlab中打开一个类似以下内容的文本文件: (1.234,2.345,3.456) (1.111,2.222,3.333) (5.432,4.321,5.432) 我试过dlmread、fscanf和importdata,但似乎都不起作用。我正在尝试将此文件读入3x3数组,但我不确定如何执行。注意:已更新以换行符分隔的数据字段 这里有一个不雅观的解决方案,假设文本文件在当前目录中被称为test.txt % Read the text file into a string:
(1.234,2.345,3.456)
(1.111,2.222,3.333)
(5.432,4.321,5.432)
我试过dlmread
、fscanf
和importdata
,但似乎都不起作用。我正在尝试将此文件读入3x3数组,但我不确定如何执行。注意:已更新以换行符分隔的数据字段
这里有一个不雅观的解决方案,假设文本文件在当前目录中被称为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
您能否向我们展示您实际尝试过的
fscanf
代码?为此,请展示您的所有尝试,并详细解释每个尝试的问题所在。