Matlab: Select a variable name and find the corresponding value

9 views (last 30 days)
Can somebody explain me how i get some specific values after the = sign? The input File is a .subvar file format. I dont know how to jump in the right row and column to get the value. Do you have a matlab tutorial link for such a problem.
I need for example two specific values (after the = sign): The value of $_Wk1_lr_m and $_Wk1_voll_m
Result: 15601 and 33690
!file.version=1.543!
! Test
subvargroup.begin ($G_Wk1)
subvar( $_Wk1_lr_C_x, str = ' 0.019 ' )
subvar( $_Wk1_lr_m, str = ' 15601 ' ) ! [kg] lr
subvar( $_Wk1_lr_C_y, str = '-0.007 ' )
subvar( $_Wk1_lr_C_z, str = ' 1.644 ' )
subvar( $_Wk1_voll_m, str = ' 33690 ' ) ! [kg] voll
subvargroup.end ($G_Wk1)

Accepted Answer

Cedric
Cedric on 16 Apr 2020
Edited: Cedric on 16 Apr 2020
Here is one way:
content = fileread( 'myTextFile.txt' ) ;
match = regexp( content, '(?<=\$_Wk1_lr_m,\s+str\s*=\s*''\s*)[^\s'']+', 'match' ) ;
Wk1_lr_m = str2double( match ) ;
match = regexp( content, '(?<=\$_Wk1_voll_m,\s+str\s*=\s*''\s*)[^\s'']+', 'match' ) ;
Wk1_voll_m = str2double( match ) ;
Note that:
  • If there are multiple subgroups in your file, you will get vectors with all the values for these variables.
  • There are many possible approaches that we can adapt to your specific use-case if you provide more information.
We can imagine all sorts of patterns. This one does the following:
  • Match one or more characters that are neither a white-space nor a single quote: [^\s']+ (the ' must be doubled not to interfer with MATLAB char array delimiters)
  • Preceeded by (?<=..) (this is a look-backward) the literal \$_Wk1_voll_m, ($ must be escaped) followed by one or more white-space \s+, followed by the literal str, followed by one or more white-space, followed by the literal ' followed by zero or more white-spaces \s*.
  4 Comments
Billy Jones
Billy Jones on 29 Apr 2020
Thank you so much for your long answer. I have translated your answer to my mother language. I understand most of your explanation. But for me as a beginner its hard to search for bugs in my code. If i use this input your code works not anymore.... Can you help me the last time .....
!file.version=1.3!
! Wagenkas
subvargroup.begin ($G_Wk)
subvar( $_Wk_leer_m, str = ' 15601 ' ) ! [kg] le
subvar( $_Wk_leer_CoG_x, str = ' 0.019 ' )
subvar( $_Wk_leer_CoG_y, str = '-0.007 ' )
subvar( $_Wk_leer_CoG_z, str = ' 1.644 ' )
subvar( $_Wk_leer_Ixx, str = ' $G_Wk.$_Wk_leer_m * 111 ' )
subvar( $_Wk_leer_Iyy, str = ' $G_Wk.$_Wk_leer_m * 222 ' )
subvar( $_Wk_leer_Izz, str = ' $G_Wk.$_Wk_leer_m * 333 ' )
subvar( $_Wk_betrieblich_m, str = ' 27660 ' ) ! [kg] betr
subvar( $_Wk_betrieblich_CoG_x, str = ' 0.0107' )
subvar( $_Wk_betrieblich_CoG_y, str = '-0.0039 ' )
subvar( $_Wk_betrieblich_CoG_z, str = ' 1.7608 ' )
subvar( $_Wk_betrieblich_Ixx, str = ' $G_Wk.$_Wk_betrieblich_m * 444 ' )
subvar( $_Wk_betrieblich_Iyy, str = ' $G_Wk.$_Wk_betrieblich_m * 555 ' )
subvar( $_Wk_betrieblich_Izz, str = ' $G_Wk.$_Wk_betrieblich_m * 666 ' )
subvar( $_Wk_voll_m, str = ' 33690 ' ) ! [kg] Wag
subvar( $_Wk_voll_CoG_x, str = ' 0.0088 ' )
subvar( $_Wk_voll_CoG_y, str = '-0.0032 ' )
subvar( $_Wk_voll_CoG_z, str = ' 1.788 ' )
subvar( $_Wk_voll_Ixx, str = ' $G_Wk.$_Wk_voll_m * 777 ' )
subvar( $_Wk_voll_Iyy, str = ' $G_Wk.$_Wk_voll_m * 888 ' )
subvar( $_Wk_voll_Izz, str = ' $G_Wk.$_Wk_voll_m * 999 ' )
subvargroup.end ($G_Wk)
Cedric
Cedric on 29 Apr 2020
Edited: Cedric on 29 Apr 2020
No problem, and actually I made a mistake that I did not see based on the small size of the input text.
I corrected the mistake (but you have to check) and updated a couple other things in the code below:
content = fileread( 'myTextFile.txt' ) ;
match = regexp( content, '_Wk_leer_Ixx,.*?\.\$(\S+)\s+\S\s*([^'']+)', 'tokens', 'once' ) ;
factor = str2double( match{2} ) ;
pattern = ['(?<=\$', match{1}, ',\s+str\s*=\s*''\s*)[^\s'']+'] ;
match = regexp( content, pattern, 'match' ) ;
Wk_leer_Ixx = str2double( match ) * factor ;
The issue was that I was using a greedy .* (in _Wk_leer_Ixx,.*\.) that would match as many characters as possible instead of the lazy .*? (now _Wk_leer_Ixx,.*?\.) that matches as few characters as possible. REGEXP was hence going as far as possible for matching what follows in the pattern, and getting '_Wk_voll_m * 999'.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 16 Apr 2020
One simple regular expression:
str = fileread('test.subvar');
rgx = 'Wk1_(lr|voll)_m\D+(\d+)';
tkn = regexp(str,rgx,'tokens');
tkn = vertcat(tkn{:})
Giving:
tkn =
'lr' '15601'
'voll' '33690'
Which of course you can easily convert to numeric:
>> vec = str2double(tkn(:,2))
vec =
15601
33690

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!