Matlab遍历文件【2.循环读取S2P文件,解决dir乱序问题】

之前一篇博客循环读取s2p文件采用dir函数出现文件乱序问题,链接奉上:MatLab读取文件【1.循环读取S2P文件并保存数据】

试了uigetfile函数和sort_nat函数两种方法,最后使用后者完美解决。其中sort_nat函数须在目录下封装成m文件才可调用,可在Matlab官网下载该函数包,链接见sort_nat: Natural Order Sort,这里我搬运下该函数方便取用。

调用sort_nat函数解决dir读取文件乱序问题

clc
clear all 
%% 遍历s2p文件
path =  'C:\Users\zl\Desktop\Sdata\';
% files = uigetfile(dir(fullfile(path,'*.s2p')));
files= dir(fullfile(path,'*.s2p'));
files_name = sort_nat({files.name});
len = size(files);

for j=1:len
fileName =files_name{j};
src=fullfile(path,fileName);
if ~strcmp(src,'C:\Users\zl\Desktop\Sdata\'.') && ~strcmp(src,'C:\Users\zl\Desktop\Sdata\..')
  disp(src)
end
   Sdata(j)=sparameters(src);
   F{1,j} =Sdata(j).Frequencies;
   S{1,j} = Sdata(j).Parameters;
   f_points=size(Sdata(j).Frequencies);

sort_nat函数

%sort_nat具体内容
function [cs,index] = sort_nat(c,mode)
%sort_nat: Natural order sort of cell array of strings.
% usage:  [S,INDEX] = sort_nat(C)
%
% where,
%    C is a cell array (vector) of strings to be sorted.
%    S is C, sorted in natural order.
%    INDEX is the sort order such that S = C(INDEX);
%
% Natural order sorting sorts strings containing digits in a way such that
% the numerical value of the digits is taken into account.  It is
% especially useful for sorting file names containing index numbers with
% different numbers of digits.  Often, people will use leading zeros to get
% the right sort order, but with this function you don't have to do that.
% For example, if C = {'file1.txt','file2.txt','file10.txt'}, a normal sort
% will give you
%
%       {'file1.txt'  'file10.txt'  'file2.txt'}
%
% whereas, sort_nat will give you
%
%       {'file1.txt'  'file2.txt'  'file10.txt'}
%
% See also: sort

% Version: 1.4, 22 January 2011
% Author:  Douglas M. Schwarz
% Email:   dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
% Real_email = regexprep(Email,{'=','*'},{'@','.'})


% Set default value for mode if necessary.
if nargin < 2
    mode = 'ascend';
end

% Make sure mode is either 'ascend' or 'descend'.
modes = strcmpi(mode,{'ascend','descend'});
is_descend = modes(2);
if ~any(modes)
    error('sort_nat:sortDirection',...
        'sorting direction must be ''ascend'' or ''descend''.')
end

% Replace runs of digits with '0'.
c2 = regexprep(c,'\d+','0');

% Compute char version of c2 and locations of zeros.
s1 = char(c2);
z = s1 == '0';

% Extract the runs of digits and their start and end indices.
[digruns,first,last] = regexp(c,'\d+','match','start','end');

% Create matrix of numerical values of runs of digits and a matrix of the
% number of digits in each run.
num_str = length(c);
max_len = size(s1,2);
num_val = NaN(num_str,max_len);
num_dig = NaN(num_str,max_len);
for i = 1:num_str
    num_val(i,z(i,:)) = sscanf(sprintf('%s ',digruns{i}{:}),'%f');
    num_dig(i,z(i,:)) = last{i} - first{i} + 1;
end

% Find columns that have at least one non-NaN.  Make sure activecols is a
% 1-by-n vector even if n = 0.
activecols = reshape(find(~all(isnan(num_val))),1,[]);
n = length(activecols);

% Compute which columns in the composite matrix get the numbers.
numcols = activecols + (1:2:2*n);

% Compute which columns in the composite matrix get the number of digits.
ndigcols = numcols + 1;

% Compute which columns in the composite matrix get chars.
charcols = true(1,max_len + 2*n);
charcols(numcols) = false;
charcols(ndigcols) = false;

% Create and fill composite matrix, comp.
comp = zeros(num_str,max_len + 2*n);
comp(:,charcols) = double(s1);
comp(:,numcols) = num_val(:,activecols);
comp(:,ndigcols) = num_dig(:,activecols);

% Sort rows of composite matrix and use index to sort c in ascending or
% descending order, depending on mode.
[unused,index] = sortrows(comp);
if is_descend
    index = index(end:-1:1);
end
index = reshape(index,size(c));
cs = c(index);

参考资源:关于Matlab中dir读取文件时顺序问题

欢迎交流、转载、点赞~

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Murata_器件库是一种用于生成s1p和s2p文件的工具。s1p和s2p文件是一种标准的数据格式,用于描述电子元件的频率响应特性。 s1p文件是一种用于描述单端口器件(例如电阻、电感、电容等)频率响应特性的文件格式。它包含了频率和单端口S参数(散射参数)的数据。S参数是一种衡量电路中能量传输和反射的指标,对于设计和分析高频电子电路非常重要。 s2p文件则是一种用于描述双端口器件(例如放大器、滤波器等)频率响应特性的文件格式。除了频率和S参数外,s2p文件还包含了传输参数和反射参数等信息。s2p文件的数据结构更加复杂,可以用于描述双端口器件中不同端口之间的传输情况。 使用Murata_器件库可以轻松生成这些s1p和s2p文件。它提供了丰富的电子元件模型和参数设置选项,可以根据用户需求生成具有不同频率响应特性的s参数文件。这些文件可以在电路仿真软件中使用,用于分析和设计高频电子电路,提高电路的性能和可靠性。 总之,Murata_器件库是一个强大的工具,可以生成s1p和s2p文件,帮助工程师们更好地理解和分析电子器件的频率响应特性,提高电路设计的效率和质量。 ### 回答2: Murata_器件库是一个用于生成S1P和S2P文件的设备库。S1P和S2P文件是用于描述和分析无源集成电路元件参数的标准格式。 S1P文件是一种描述散射参数(S参数)的文件格式。S参数是用于描述无源电子器件转移函数的系数,例如反射系数和传输系数等。S1P文件通常包含频率响应和相位的信息,可以通过频率点和相位角来描述器件在不同频率下的性能。 S2P文件是一种描述散射矩阵参数(S参数)的文件格式。S2P文件包含器件的全部S参数信息,能够描述器件在所有频率点上的反射和传输特性。S2P文件可以通过将S参数矩阵中各个元素归一化,使其适合于一系列频率。 Murata_器件库通过生成S1P和S2P文件,提供了一种方便的方法来描述和分析Murata产品的电特性。这些文件可以在仿真软件中使用,例如微波电路设计软件或系统级仿真软件。通过使用这些文件和Murata_器件库中的元件模型,设计人员可以更准确地预测和分析Murata器件在不同电路和系统中的性能。 总之,Murata_器件库能够生成S1P和S2P文件,为设计人员提供方便的工具来描述和分析Murata产品的电特性,并帮助他们更好地设计和优化电路和系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值