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