필터 지우기
필터 지우기

spliting up a char array

조회 수: 6 (최근 30일)
me
me 2015년 11월 7일
편집: Stephen23 2015년 11월 8일
If I have a char variable... say x=2.1*3C how can i get 2.1 by itself as its own variable
  댓글 수: 2
me
me 2015년 11월 7일
I read a large text file full of GPS data and divided the string up by the using xc=textscan(scol{k,1}, '%s', 'Delimiter',','). Its in a for look and it scans sentences that look like this: $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*3C
Anyway, one of the points in this cell(scol{1,1}{18,1}) looks like this 2.1*3C but for my project I only need the numbers before the *. I have no idea how to get it or how I could have written my code differently.
me
me 2015년 11월 7일
i ment for loop not 'for look'

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2015년 11월 7일
편집: Stephen23 2015년 11월 8일
One answer to your question is to use indexing:
>> S = '2.1*3C'
S =
2.1*3C
>> T = S(1:3)
T =
2.1
If you want this as a numeric value then:
str2double(S(1:3))
Although there are many possible answers to your question, I suspect that this whole thing could be avoided by better planning and data concept. Could you please give us more information about what you are trying to do with the 2.1, and where this string comes from. Probably passing the data in something more suitable than a string would make your programming much simpler and more reliable.
  댓글 수: 2
me
me 2015년 11월 7일
Thank you! that worked perfectly.
Image Analyst
Image Analyst 2015년 11월 7일
If it was "perfect" then you can also "vote" for the answer to give him double reputation points. That'd be a nice thing to do for him.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 11월 7일
Some of the millions of ways:
% Set up (if the x= is part of the string):
myString = 'x=2.1*3C'
% Find the equal sign:
equalIndex = strfind(myString, '=');
% Find the *:
starIndex = strfind(myString, '*');
% Now get 2.1 in its own character variable:
itsOwnVariable = myString(equalIndex+1:starIndex-1) % as another string
% Now get 2.1 in its own character variable:
itsOwnVariable = str2double(myString(equalIndex+1:starIndex-1)) % As a double
% Set up with no x= in the string:
myString = '2.1*3C'
% Now get 2.1 in its own character variable:
itsOwnVariable = myString(1:3) % as another string
% Now get 2.1 in its own character variable:
itsOwnVariable = sscanf(myString, '%f') % As a double

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by