我是靠谱客的博主 超级魔镜,这篇文章主要介绍matlab的一些记录1,现在分享给大家,希望可以做个参考。

下面代码是批量处理csv文件,读取里面的数据并做相关成图

  1. 获取文件路径
  2. 匹配文件名
  3. 读取文件中的某列数据中的某行到某行
  4. 多个数据成图在一起
  5. 生成变量,便于使用Curve Fitting工具,观察其线性相关程度

数据说明

首先是一系列CSV文件,是阻抗分析仪测量传感器Cs-Rs模型的数据,当然可以通过公式转换成相位幅值,如下图所示

然后是csv文件

下面代码分别是main_Relay.m,initialise_path.m,csv2csrs.m文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
clear load('f.mat') f=f(1:501); list=dir('D:projectmatlabA13airrelay*.CSV'); for i=1:length(list) B = regexp(list(i).name,'w*d','Match'); C(i)=str2double(B); end C = sort(C); C_relay=zeros(501,length(C)); R_relay=zeros(501,length(C)); for i=1:length(C) filename=strcat('D:projectmatlabA13airrelay',num2str(C(i)),'.csv'); [Cs,Rs] = csv2csrs(filename); C_relay(:,i)=Cs; R_relay(:,i)=Rs; end C_direct=zeros(501,length(C)); R_direct=zeros(501,length(C)); for i=1:length(C) filename=strcat('D:projectmatlabA13airDirect',num2str(C(i)),'.csv'); [Cs,Rs] = csv2csrs(filename); C_direct(:,i)=Cs; R_direct(:,i)=Rs; end figure for i = 10:25:501 plot(C_direct(i,:),C_relay(i,:)); hold all end x=C_relay(310,:); y=C_direct(310,:); delta_C=C_relay-C_direct; delta_R=R_relay-R_direct;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function Untitled = importfile(filename, startRow, endRow) %IMPORTFILE Import numeric data from a text file as a matrix. % UNTITLED = IMPORTFILE(FILENAME) Reads data from text file FILENAME for % the default selection. % % UNTITLED = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows % STARTROW through ENDROW of text file FILENAME. % % Example: % Untitled = importfile('150.CSV', 5, 505); % % See also TEXTSCAN. % Auto-generated by MATLAB on 2019/12/14 17:51:15 %% Initialize variables. if nargin<=2 startRow = 5; endRow = 505; end %% Read columns of data as text: % For more information, see the TEXTSCAN documentation. formatSpec = '%20s%21s%s%[^nr]'; %% Open the text file. fileID = fopen(filename,'r'); %% Read columns of data according to the format. % 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. textscan(fileID, '%[^nr]', startRow(1)-1, 'WhiteSpace', '', 'ReturnOnError', false); dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'ReturnOnError', false, 'EndOfLine', 'rn'); for block=2:length(startRow) frewind(fileID); textscan(fileID, '%[^nr]', startRow(block)-1, 'WhiteSpace', '', 'ReturnOnError', false); dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'ReturnOnError', false, 'EndOfLine', 'rn'); for col=1:length(dataArray) dataArray{col} = [dataArray{col};dataArrayBlock{col}]; end end %% Close the text file. fclose(fileID); %% Convert the contents of columns containing numeric text to numbers. % Replace non-numeric text with NaN. raw = repmat({''},length(dataArray{1}),length(dataArray)-1); for col=1:length(dataArray)-1 raw(1:length(dataArray{col}),col) = mat2cell(dataArray{col}, ones(length(dataArray{col}), 1)); end numericData = NaN(size(dataArray{1},1),size(dataArray,2)); for col=[1,2,3] % Converts text in the input cell array to numbers. Replaced non-numeric % text 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*[eEdD]{0,1}[-+]*d*[i]{0,1})|([-]*(d+[,]*)*[.]{1,1}d+[eEdD]{0,1}[-+]*d*[i]{0,1}))(?<suffix>.*)'; try result = regexp(rawData(row), regexstr, 'names'); numbers = result.numbers; % Detected commas in non-thousand locations. invalidThousandsSeparator = false; if numbers.contains(',') thousandsRegExp = '^d+?(,d{3})*.{0,1}d*$'; if isempty(regexp(numbers, thousandsRegExp, 'once')) numbers = NaN; invalidThousandsSeparator = true; end end % Convert numeric text to numbers. if ~invalidThousandsSeparator numbers = textscan(char(strrep(numbers, ',', '')), '%f'); numericData(row, col) = numbers{1}; raw{row, col} = numbers{1}; end catch raw{row, col} = rawData{row}; end end end %% Create output variable Untitled = table; Untitled.KeysightTechnologi = cell2mat(raw(:, 1)); Untitled.esE4990AMY54302337 = cell2mat(raw(:, 2)); Untitled.A0302 = cell2mat(raw(:, 3));
复制代码
1
2
3
4
5
6
7
function [Cs,Rs] = csv2csrs(filename) Z_table=importfile(filename); Cs=table2array(Z_table(:,2)); Rs=table2array(Z_table(:,3)); end

下面是通过matlab的绘图工具直接对工作区的变量成图。

 

最后

以上就是超级魔镜最近收集整理的关于matlab的一些记录1的全部内容,更多相关matlab内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部