声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1405|回复: 2

[编程技巧] 含单位的文本文件如何读入matlab

[复制链接]
发表于 2014-4-18 10:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
本帖最后由 牛小贱 于 2014-4-24 20:16 编辑

现有文本文件data.txt,其格式如下

1.0m/s  2,5,6
2.0m/s  4,8
3.0m/s  6,4,8
……
读入文件后数组格式为:
1.0  2
1.0  5
1.0  6
2.0  4
2.0  8
3.0  6
3.0  4
3.0  8
......
谢谢!

data.txt

41 Bytes, 下载次数: 1

回复
分享到:

使用道具 举报

发表于 2014-4-19 22:12 | 显示全部楼层
用textscan读取文件,读完是cell格式,根据需要处理就行了。我最近几天比较忙,楼主可以先参考一下,我有时间再仔细看一下!!
相关资料链接:
matlab 中的textscan

使用文本文件(.txt)进行数据存取的技巧总结(相当的经典)

评分

1

查看全部评分

发表于 2014-4-23 11:42 | 显示全部楼层
调用函数:

  1. function data = importfile(filename, startRow, endRow)
  2. %IMPORTFILE Import numeric data from a text file as a matrix.
  3. %   DATA = IMPORTFILE(FILENAME) Reads data from text file FILENAME for the
  4. %   default selection.
  5. %
  6. %   DATA = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows
  7. %   STARTROW through ENDROW of text file FILENAME.
  8. %
  9. % Example:
  10. %   data = importfile('data.txt', 1, 3);
  11. %
  12. %    See also TEXTSCAN.

  13. % Auto-generated by MATLAB on 2014/04/22 17:04:05

  14. %% Initialize variables.
  15. delimiter = ' ';
  16. if nargin<=2
  17.     startRow = 1;
  18.     endRow = inf;
  19. end

  20. %% Read columns of data as strings:
  21. % For more information, see the TEXTSCAN documentation.
  22. formatSpec = '%s%s%s%s%[^\n\r]';

  23. %% Open the text file.
  24. fileID = fopen(filename,'r');

  25. %% Read columns of data according to format string.
  26. % This call is based on the structure of the file used to generate this
  27. % code. If an error occurs for a different file, try regenerating the code
  28. % from the Import Tool.
  29. dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
  30. for block=2:length(startRow)
  31.     frewind(fileID);
  32.     dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
  33.     for col=1:length(dataArray)
  34.         dataArray{col} = [dataArray{col};dataArrayBlock{col}];
  35.     end
  36. end

  37. %% Close the text file.
  38. fclose(fileID);

  39. %% Convert the contents of columns containing numeric strings to numbers.
  40. % Replace non-numeric strings with NaN.
  41. raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
  42. for col=1:length(dataArray)-1
  43.     raw(1:length(dataArray{col}),col) = dataArray{col};
  44. end
  45. numericData = NaN(size(dataArray{1},1),size(dataArray,2));

  46. for col=[1,2,3,4]
  47.     % Converts strings in the input cell array to numbers. Replaced non-numeric
  48.     % strings with NaN.
  49.     rawData = dataArray{col};
  50.     for row=1:size(rawData, 1);
  51.         % Create a regular expression to detect and remove non-numeric prefixes and
  52.         % suffixes.
  53.         regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
  54.         try
  55.             result = regexp(rawData{row}, regexstr, 'names');
  56.             numbers = result.numbers;
  57.             
  58.             % Detected commas in non-thousand locations.
  59.             invalidThousandsSeparator = false;
  60.             if any(numbers==',');
  61.                 thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*';
  62.                 if isempty(regexp(thousandsRegExp, ',', 'once'));
  63.                     numbers = NaN;
  64.                     invalidThousandsSeparator = true;
  65.                 end
  66.             end
  67.             % Convert numeric strings to numbers.
  68.             if ~invalidThousandsSeparator;
  69.                 numbers = textscan(strrep(numbers, ',', ''), '%f');
  70.                 numericData(row, col) = numbers{1};
  71.                 raw{row, col} = numbers{1};
  72.             end
  73.         catch me
  74.         end
  75.     end
  76. end


  77. %% Replace non-numeric cells with NaN
  78. R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
  79. raw(R) = {NaN}; % Replace non-numeric cells

  80. %% Create output variable
  81. data = cell2mat(raw);
复制代码
主程序:
  1. a= blkproc(importfile('data.txt', 1, 4), [1 1], @(x)repmat(x, 3, 1));
  2. b=a(:,1);m=a(:,2:4);c=diag(m(1:3,:));d=diag(m(4:6,:));e=diag(m(7:9,:));
  3. f=[c; d; e];
  4. M=[b f]
复制代码

结果:
PS:这个程序有点复杂,而且结果也不是很理想。希望对楼主有所帮助!!

搜狗截图20140423114143.png

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-29 19:22 , Processed in 0.068447 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表