MATLAB数据处理读取csv文件,批量命名,文件自然顺序读取

您所在的位置:网站首页 matlab如何读取csv文件中的数据 MATLAB数据处理读取csv文件,批量命名,文件自然顺序读取

MATLAB数据处理读取csv文件,批量命名,文件自然顺序读取

#MATLAB数据处理读取csv文件,批量命名,文件自然顺序读取| 来源: 网络整理| 查看: 265

MATLAB进行数据分析及处理,常常会需要访问各种文件,以及批量处理。下面总结一下常用的数据处理函数及方式。

1,读取当前文件夹下所有csv文件,并将每个文件中的特定数据组成一个矩阵,并组合。

file=dir('KH\*.csv'); %读取KH文件夹下全部csv文档 [~, ind] = sort([file(:).datenum], 'ascend'); %读物的文件按照时间顺序排列 filenew = file(ind); %更新文件夹顺序 filename={filenew.name}'; Fold = {filenew.folder}'; fileall = strcat(Fold,'\',filename); %将文件路径组合

将文件夹中的每个cvs文件取出特定数据,组成矩阵

Num = length(fileall); for i = 1:Num fid = fopen(fileall{i}); cellc = textscan(fid,'%f %f %f %f %s %f %s %f %f %f %f %f %f %f %s %*[^\n]','HeaderLines',6,'Delimiter',','); %采用textscan的方式读取每个csv文件的内容。 fclose(fid); dataall = cell2mat(cellc(1,8:14));%获取有用的数据 end

2,批量将某一文件夹下mat文件重新命名并放置在特定文件夹下。此目的可以分为两个步骤:a,对mat文件批量命名,一般采样eval函数(matlab中尽量避免此函数,不方便调试效率也较低)。b,批量保存文件。

DOCman = "newdata"; saldir = 'F:\data_source\'; Newdir = mkdir(saldir,DOCman); %根据需求创建文件夹 Num = length(data); for i = 1:Num var_data = ['data_',num2str(i)]; savePath =strcat(saldir,DOCman,'\','data_',num2str(i), '.mat');%确定保存路径及保存文件方式 eval(['data_',num2str(i),'=','data',';']); %用eval批量命名 save(savePath,var_data); end

3,批量读取文件夹下所有文件,并将文件按顺序排序,windows系统下可以自动按照1,2,3...10,11...顺序排序文件 ,matlab批量读取的时候并不一定按照这种方式,所以需要自己处理,matlab自带sort函数可以进行排序,但是效果不佳。可以编辑下列m文件。

function [X,ndx,dbg] = natsort(X,rgx,varargin) % Alphanumeric / Natural-Order sort the strings in a cell array of strings (1xN char). % % (c) 2012-2019 Stephen Cobeldick % % Alphanumeric sort a cell array of strings: sorts by character order and % also by the values of any number substrings. Default: match all integer % number substrings and perform a case-insensitive ascending sort. % %%% Example: % >> X = {'x2', 'x10', 'x1'}; % >> sort(X) % ans = 'x1' 'x10' 'x2' % >> natsort(X) % ans = 'x1' 'x2' 'x10' % %%% Syntax: % Y = natsort(X) % Y = natsort(X,rgx) % Y = natsort(X,rgx,) % [Y,ndx,dbg] = natsort(X,...) % % To sort filenames or filepaths use NATSORTFILES (FEX 47434). % To sort the rows of a cell array of strings use NATSORTROWS (FEX 47433). % %% Number Substrings %% % % By default consecutive digit characters are interpreted as an integer. % Specifying the optional regular expression pattern allows the numbers to % include a +/- sign, decimal digits, exponent E-notation, quantifiers, % or look-around matching. For information on defining regular expressions: % http://www.mathworks.com/help/matlab/matlab_prog/regular-expressions.html % % The number substrings are parsed by SSCANF into numeric values, using % either the *default format '%f' or the user-supplied format specifier. % % This table shows examples of regular expression patterns for some common % notations and ways of writing numbers, with suitable SSCANF formats: % % Regular | Number Substring | Number Substring | SSCANF % Expression: | Match Examples: | Match Description: | Format Specifier: % ==============|==================|===============================|================== % * \d+ | 0, 123, 4, 56789 | unsigned integer | %f %i %u %lu % --------------|------------------|-------------------------------|------------------ % [-+]?\d+ | +1, 23, -45, 678 | integer with optional +/- sign| %f %i %d %ld % --------------|------------------|-------------------------------|------------------ % \d+\.?\d* | 012, 3.45, 678.9 | integer or decimal | %f % (\d+|Inf|NaN) | 123, 4, NaN, Inf | integer, Inf, or NaN | %f % \d+\.\d+e\d+ | 0.123e4, 5.67e08 | exponential notation | %f % --------------|------------------|-------------------------------|------------------ % 0[0-7]+ | 012, 03456, 0700 | octal notation & prefix | %o %i % [0-7]+ | 12, 3456, 700 | octal notation | %o % --------------|------------------|-------------------------------|------------------ % 0X[0-9A-F]+ | 0X0, 0X3E7, 0XFF | hexadecimal notation & prefix | %x %i % [0-9A-F]+ | 0, 3E7, FF | hexadecimal notation | %x % --------------|------------------|-------------------------------|------------------ % 0B[01]+ | 0B1, 0B101, 0B10 | binary notation & prefix | %b (not SSCANF) % [01]+ | 1, 101, 10 | binary notation | %b (not SSCANF) % --------------|------------------|-------------------------------|------------------ % %% Debugging Output Array %% % % The third output is a cell array , to check if the numbers have % been matched by the regular expression and converted to numeric % by the SSCANF format. The rows of are linearly indexed from , % even columns contain numbers, odd columns contain split substrings: % % >> [~,~,dbg] = natsort(X) % dbg = % 'x' [ 2] % 'x' [10] % 'x' [ 1] % %% Examples %% % %%% Multiple integers (e.g. release version numbers): % >> A = {'v10.6', 'v9.10', 'v9.5', 'v10.10', 'v9.10.20', 'v9.10.8'}; % >> sort(A) % ans = 'v10.10' 'v10.6' 'v9.10' 'v9.10.20' 'v9.10.8' 'v9.5' % >> natsort(A) % ans = 'v9.5' 'v9.10' 'v9.10.8' 'v9.10.20' 'v10.6' 'v10.10' % %%% Integer, decimal, NaN, or Inf numbers, possibly with +/- signs: % >> B = {'test+NaN', 'test11.5', 'test-1.4', 'test', 'test-Inf', 'test+0.3'}; % >> sort(B) % ans = 'test' 'test+0.3' 'test+NaN' 'test-1.4' 'test-Inf' 'test11.5' % >> natsort(B, '[-+]?(NaN|Inf|\d+\.?\d*)') % ans = 'test' 'test-Inf' 'test-1.4' 'test+0.3' 'test11.5' 'test+NaN' % %%% Integer or decimal numbers, possibly with an exponent: % >> C = {'0.56e007', '', '43E-2', '10000', '9.8'}; % >> sort(C) % ans = '' '0.56e007' '10000' '43E-2' '9.8' % >> natsort(C, '\d+\.?\d*([eE][-+]?\d+)?') % ans = '' '43E-2' '9.8' '10000' '0.56e007' % %%% Hexadecimal numbers (with '0X' prefix): % >> D = {'a0X7C4z', 'a0X5z', 'a0X18z', 'a0XFz'}; % >> sort(D) % ans = 'a0X18z' 'a0X5z' 'a0X7C4z' 'a0XFz' % >> natsort(D, '0X[0-9A-F]+', '%i') % ans = 'a0X5z' 'a0XFz' 'a0X18z' 'a0X7C4z' % %%% Binary numbers: % >> E = {'a11111000100z', 'a101z', 'a000000000011000z', 'a1111z'}; % >> sort(E) % ans = 'a000000000011000z' 'a101z' 'a11111000100z' 'a1111z' % >> natsort(E, '[01]+', '%b') % ans = 'a101z' 'a1111z' 'a000000000011000z' 'a11111000100z' % %%% Case sensitivity: % >> F = {'a2', 'A20', 'A1', 'a10', 'A2', 'a1'}; % >> natsort(F, [], 'ignorecase') % default % ans = 'A1' 'a1' 'a2' 'A2' 'a10' 'A20' % >> natsort(F, [], 'matchcase') % ans = 'A1' 'A2' 'A20' 'a1' 'a2' 'a10' % %%% Sort order: % >> G = {'2', 'a', '', '3', 'B', '1'}; % >> natsort(G, [], 'ascend') % default % ans = '' '1' '2' '3' 'a' 'B' % >> natsort(G, [], 'descend') % ans = 'B' 'a' '3' '2' '1' '' % >> natsort(G, [], 'num


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3