フィルターのクリア

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 Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by