Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract the values from a file name?

I have multiple files that has been named after the same way : 'RefBu12CyclesAmpl0.20Freq2.25VR0.0000Dist4cmSilence25000Fs25MHz' . I tried to make a function that returns the values and make the difference between strings and digits, but I can make it return the correct values.

Any ideas?

function [File] = get_somenthing(file_name)   
expression = '(?<Cycles>\d+)(?<Ampl>\D\S.+)(?<Freq>\d+)(?<VR>\d\S+)(?<Dist>\d.+)(?<Silence>\d+)(?<Fs>\d)'   
File = regexp(file_name,expression,'names')   
like image 897
Andreea Avatar asked Dec 05 '25 08:12

Andreea


1 Answers

The following regular expresion matches digits optionally followed by a dot and digits. I assume the numbers are real, don't include signs, and cannot use scientific notation:

s = 'RefBu12CyclesAmpl0.20Freq2.25VR0.0000Dist4cmSilence25000Fs25MHz';
values_str = regexp(s, '\d+(\.\d+)?', 'match');
values = str2double(values_str);

This gives

>> values_str
values_str =
  1×7 cell array
    {'12'}    {'0.20'}    {'2.25'}    {'0.0000'}    {'4'}    {'25000'}    {'25'}

>> values
values =
   1.0e+04 *
   0.001200000000000   0.000020000000000   0.000225000000000                   0   0.000400000000000   2.500000000000000   0.002500000000000
like image 136
Luis Mendo Avatar answered Dec 07 '25 23:12

Luis Mendo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!