Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have a list of strings in a char array:

'gvs(0.000000000000000e+000, 1.601985139535780e+002)'
'gvs(-5.000000000000000e-005, 1.365231866954370e+002)'
'gvs(-1.000000000000000e-004, 1.169431404340180e+002)'
'gvs(-5.000000000000000e-004, 3.187711314514890e+001)'
'gvs(-2.000000000000000e-004, 8.589930648472340e+001)'

Which I am trying to convert to an array of just the numbers (ignoring gvs, the comma and the brackets), but I can't quite work out what I'm doing wrong?

cols = length(Variables) + length(Parameters);
% currently unused
rows = length(Results);

for a = 1:rows;
    Res(a,:) = sscanf ((Results{a,1}(1,:)),'%*s %f %f');
end

I've also tried textscan, but I can't get that to work right either

for a = 1:rows;
    Res = cell (textscan ((Results{a,1}(1,:)),'%*s %f %f','Delimiter', {'(',' '},'MultipleDelimsAsOne',1));
end

Any help much appreciated!

Thanks

share|improve this question
    
The problem here is that %*s reads characters until the first whitespace. What you need is to explicitly match the gvs string (including the parentheses). Also, there's really no point for the (1, :) indexing, just use Results{a, 1} instead. – Eitan T Jul 11 '13 at 11:20
1  
Ah, brilliant, very helpful! – LADransfield Jul 11 '13 at 11:58

3 Answers 3

up vote 2 down vote accepted

Replace

Res(a,:) = sscanf ((Results{a,1}(1,:)),'%*s %f %f');

with

Res(a,:) = sscanf ((Results{a,1}(1,:)),'gvs(%f, %f)');
share|improve this answer
    
When I do that I get "Conversion to cell from double is not possible." error? If I remove the (a,:) after Res, then I get: Res = -0.0002 85.8993 Which seems to be getting there, but only the last row... – LADransfield Jul 11 '13 at 11:11
2  
Ah, I fixed it Res{a,1} = sscanf ((Results{a,1}),'gvs(%f, %f)')'; – LADransfield Jul 11 '13 at 11:57

Assuming you have a char array (not a cellstring):

s = ['gvs( 0.000000000000000e+000, 1.601985139535780e+002)'
     'gvs(-5.000000000000000e-005, 1.365231866954370e+002)'
     'gvs(-1.000000000000000e-004, 1.169431404340180e+002)'
     'gvs(-5.000000000000000e-004, 3.187711314514890e+001)'
     'gvs(-2.000000000000000e-004, 8.589930648472340e+001)']

Then you can simply textscan():

data = textscan(s','gvs(%f%f)','CollectOutput',1,'Delimiter',',');
data = data{1}
data =
         0  160.1985
   -0.0001  136.5232
   -0.0001  116.9431
   -0.0005   31.8771
   -0.0002   85.8993

If s is a cellstring, then before calling textscan, convert to char():

s = char(s);
share|improve this answer
    
I think Results is a cell array, note the curly braces in the question. – Eitan T Jul 11 '13 at 11:17

Considering that your string is almost in MATLAB-array compatible format, you could commit a sin and use evalc:

>> s = {
    'gvs( 0.000000000000000e+000, 1.601985139535780e+002)'
    'gvs(-5.000000000000000e-005, 1.365231866954370e+002)'
    'gvs(-1.000000000000000e-004, 1.169431404340180e+002)'
    'gvs(-5.000000000000000e-004, 3.187711314514890e+001)'
    'gvs(-2.000000000000000e-004, 8.589930648472340e+001)'};

>> C = evalc(['[' regexprep([s{:}], {'gvs\(' '\)'}, {'' ';'}) ']'])

ans =
                         0    1.601985139535780e+002
   -5.000000000000000e-005    1.365231866954370e+002
   -1.000000000000000e-004    1.169431404340180e+002
   -5.000000000000000e-004    3.187711314514890e+001
   -2.000000000000000e-004    8.589930648472340e+001
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.