简体   繁体   中英

Matlab: Select a variable name and find the corresponding value

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

!file.version=1.543! 
! Testautomatisch 

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)

在此处输入图像描述

What are the first steps to get the right row and the right column? Thank you and stay at home:)

read the file line by line, match the line format and extract the values using regular expression regexp

fid=fopen('mydata.subvars','rt');
res=struct;
while(~feof(fid))
  line=fgetl(fid);
  if(regexp(line,'^\s*subvar\(','once'))
     val=regexp(line,'\$_(\w+),\s*str\s*=\s*''\s*([0-9.-]+)\s*','tokens');
     if(length([val{:}])==2)
        res.(val{1}{1})=str2num(val{1}{2});
     end
  end
end
fclose(fid);

here is the result

>> res

res = 

    Wk1_lr_C_x: 0.0190
      Wk1_lr_m: 15601
    Wk1_lr_C_y: -0.0070
    Wk1_lr_C_z: 1.6440
    Wk1_voll_m: 33690

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM