我是靠谱客的博主 超级魔镜,最近开发中收集的这篇文章主要介绍matlab的一些记录1,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

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

数据说明

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

然后是csv文件

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

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;
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));

function [Cs,Rs] = csv2csrs(filename) 

Z_table=importfile(filename);
Cs=table2array(Z_table(:,2));
Rs=table2array(Z_table(:,3));

end

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

 

最后

以上就是超级魔镜为你收集整理的matlab的一些记录1的全部内容,希望文章能够帮你解决matlab的一些记录1所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部