Reputation: 747
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
Upvotes: 0
Views: 384
Reputation: 38032
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
Upvotes: 0
Reputation: 10676
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);
Upvotes: 2
Reputation: 9864
Replace
Res(a,:) = sscanf ((Results{a,1}(1,:)),'%*s %f %f');
with
Res(a,:) = sscanf ((Results{a,1}(1,:)),'gvs(%f, %f)');
Upvotes: 2