How would I go about separating each of the numerical values from this string, and create a named variable from each?

1 次查看(过去 30 天)
Afternoon,
How would I go about separating each of the numerical values from this string, and create a named variable from each?
Current effort is below:
Str = 'Average 0: ASM=5, DEP=34m, AV=652632.44';
Str(strfind(Str, '=')) = [];
Key1 = 'AMS';
Key2 = 'DEP';
Key3 = 'AV';
Index = strfind(Str, Key1);
Value = sscanf(Str(Index(1) + length(Key1):end), '%g', 1);
Error message when run:
Index exceeds matrix dimensions.
Error in glider_transmitted_data (line 33)
Value = sscanf(Str(Index(1) + length(Key1):end), '%g', 1);
Many thanks,
Alex

回答(1 个)

Roger
Roger 2015-4-6
编辑:Roger 2015-4-7
Str = 'Average 0: ASM=5, DEP=34m, AV=652632.44';
rcell=strsplit(Str,':');s2=rcell{2};rcell2=strsplit(s2,',');
Average0=str2num(rcell{1}(end-1:end));
r3=strsplit(rcell2{1},'=');
ASM=str2num(r3{2});
DEP=strsplit(rcell2{2},'=');
AV=str2num(strsplit(rcell2{3},'='));
  1 个评论
Alexander Vincent
Cheers Roger,
Getting the following error:
Error using str2num (line 32) Requires string or character array input.
Error in glider_transmitted_data (line 39) ASM = str2num(strsplit(rcell2{1},'='));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by