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

1 view (last 30 days)
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

Answers (1)

Roger
Roger on 6 Apr 2015
Edited: Roger on 7 Apr 2015
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 Comment
Alexander Vincent
Alexander Vincent on 6 Apr 2015
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},'='));

Sign in to comment.

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!