含单位的文本文件如何读入matlab
本帖最后由 牛小贱 于 2014-4-24 20:16 编辑现有文本文件data.txt,其格式如下
1.0m/s2,5,6
2.0m/s4,8
3.0m/s6,4,8
……
读入文件后数组格式为:
1.02
1.05
1.06
2.04
2.08
3.06
3.04
3.08
......
谢谢!
用textscan读取文件,读完是cell格式,根据需要处理就行了。我最近几天比较忙,楼主可以先参考一下,我有时间再仔细看一下!!
相关资料链接:
matlab 中的textscan ;
使用文本文件(.txt)进行数据存取的技巧总结(相当的经典)
调用函数:
function data = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as a matrix.
% DATA = IMPORTFILE(FILENAME) Reads data from text file FILENAME for the
% default selection.
%
% DATA = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows
% STARTROW through ENDROW of text file FILENAME.
%
% Example:
% data = importfile('data.txt', 1, 3);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2014/04/22 17:04:05
%% Initialize variables.
delimiter = ' ';
if nargin<=2
startRow = 1;
endRow = inf;
end
%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
for col=1:length(dataArray)
dataArray{col} = ;
end
end
%% Close the text file.
fclose(fileID);
%% Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*{0,1}[-+]*\d*{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+{0,1}[-+]*\d*{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
end
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%% Create output variable
data = cell2mat(raw);
主程序:a= blkproc(importfile('data.txt', 1, 4), , @(x)repmat(x, 3, 1));
b=a(:,1);m=a(:,2:4);c=diag(m(1:3,:));d=diag(m(4:6,:));e=diag(m(7:9,:));
f=;
M=
结果:
PS:这个程序有点复杂,而且结果也不是很理想。希望对楼主有所帮助!!
页:
[1]