【问题标题】:How to get numeric value(Decimals) from a string name如何从字符串名称中获取数值(小数)
【发布时间】:2018-10-08 18:59:21
【问题描述】:

我目前有一个从 P0.01.mat-P10.mat 标记的 mat 文件列表,并且想获取所选 .mat 文件的数值。我目前所拥有的只是从 P1.mat 到 P10.mat。当我为一串 P0.01.mat 运行它时,它返回数字 1。

    Files=dir(fullfile(datapath,'*.mat'));
    numfiles=length(Files);
    results=cell(numfiles,1);
    for k = 1:numfiles
    results{k}=Files(k).name; % lists all the names available
    end
    Ace=[results];
    A=Ace(1);  %selects the string 

     B = regexp(A,'\d*','Match');  %gets the numerals in the string 
     for ii= 1:length(B)
         if ~isempty(B{ii})
         Num(ii,1)=str2double(B{ii}(end));
         else
         Num(ii,1)=NaN;
         end
   end
   Num    

【问题讨论】:

    标签: matlab mat-file


    【解决方案1】:

    要匹配带有可选小数点的数字,请使用正则表达式字符串[0-9]*\.?[0-9]+

    B = regexp(A,'[0-9]*\.?[0-9]+','Match');  %gets the numerals in the string
    

    参考:A: Parsing scientific notation sensibly?(其中包含用于更复杂的浮点值表示的正则表达式)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      相关资源
      最近更新 更多