필터 지우기
필터 지우기

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

조회 수: 2 (최근 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
Alexander Vincent 2015년 4월 6일
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