Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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

Antworten (1)

Roger
Roger am 6 Apr. 2015
Bearbeitet: Roger am 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 Kommentar
Alexander Vincent
Alexander Vincent am 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},'='));

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by