Matlab:选择一个变量名称并找到相应的值 [英] Matlab: Select a variable name and find the corresponding value

查看:717
本文介绍了Matlab:选择一个变量名称并找到相应的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释在=号后如何获得一些特定值吗?输入文件是.subvar文件格式. 我不知道如何跳到正确的行和列来获取值.您是否有用于解决此类问题的matlab教程链接.

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.

例如,我需要两个特定值(在=符号之后): $ _Wk1_lr_m和$ _Wk1_voll_m的值

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 :)

推荐答案

逐行读取文件,匹配行格式并使用正则表达式regexp

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);

这是结果

>> 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

这篇关于Matlab:选择一个变量名称并找到相应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆