0

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

1
  • 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. Commented Jul 11, 2013 at 11:20

3 Answers 3

2

Replace

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

with

Res(a,:) = sscanf ((Results{a,1}(1,:)),'gvs(%f, %f)');
Sign up to request clarification or add additional context in comments.

2 Comments

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...
Ah, I fixed it Res{a,1} = sscanf ((Results{a,1}),'gvs(%f, %f)')';
2

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);

1 Comment

I think Results is a cell array, note the curly braces in the question.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.