spliting up a char array
Show older comments
If I have a char variable... say x=2.1*3C how can i get 2.1 by itself as its own variable
Accepted Answer
More Answers (1)
Image Analyst
on 7 Nov 2015
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
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!