Matlab: Select a variable name and find the corresponding value

조회 수: 16(최근 30일)
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
! 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)

채택된 답변

Cedric Wannaz
Cedric Wannaz 2020년 4월 16일
편집: Cedric Wannaz 2020년 4월 16일
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
Cedric Wannaz
Cedric Wannaz 2020년 4월 29일
편집: Cedric Wannaz 2020년 4월 29일
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'.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

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


Community Treasure Hunt

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

Start Hunting!

Translated by