Convert Cell String 'plus(0,0') to Num not using Eval or Str2Num

Hi Friends,
Well I have a problem that's very similar to some ohter guys using str2num() or eval() functions.
The great point is: Imagine that I have this:
for i=1:1000
C{i}='plus(0,0)';
end
And I want to retrieve the number that is generated by this function (in fact I have problem greater than this, but solving this problem can help to solve the bigger picture).
A way to solve this is:
a=ones(1000,1)
for j=1:1000
a(i,1)=eval(C{i});
end
or
a=ones(1000,1)
for j=1:1000
a(i,1)=str2num(C{i});
end
or
a=cellfun(@eval,C);
The great problem using these methods is that takes a lots of time to do this.
Someone can help me to solve this problem faster!
Thanks, Adriano

2 件のコメント

Image Analyst
Image Analyst 2013 年 5 月 21 日
How does this problem arise? In other words, why does the operation you want to perform (plus or whatever) come in as a string? It doesn't seem like the most ideal way to construct a program. Does it ever change from plus to some other operation, and if so, why? What is the "use case"?
Cristian munoz
Cristian munoz 2013 年 5 月 22 日
This problem arises when you work with Genetic Programming in Matlab. Genetic Programming is, in general, a way to produce mathematical functions that approximatte or find the solution for a problem. In this way you have strings that are matlab primitive functions (or others) that you need to eval to see if the solution finded satisfy the problem criteria.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2013 年 5 月 21 日

0 投票

regexp() to pull the operation and operands out of the string. Construct a function handle that performs that operation on the required operands. Loop calling that function.
Depending on what you are doing, you might be able to construct vectorized versions that are then applied over 1:100 .

4 件のコメント

Cristian munoz
Cristian munoz 2013 年 5 月 22 日
Hi Walter Roberson,
I will try this solution as fast as possible, and give you a feedback.
Cheers,
Adriano
Cristian munoz
Cristian munoz 2013 年 5 月 22 日
Hi Walter Robinson
Can you explain with an example. I tried to figure out what you tell me, but i cannot implement this on matlab.
Adriano
Walter Roberson
Walter Roberson 2013 年 5 月 22 日
known_ops = {'plus', 'times', 'pow'}; %known operation names
op_handles = {@plus, @times, @power}; %function handles that enact them
T = regexp(C{i}, '(\w+)\((\w+),(\w+)\)', 'match');
[tf, idx] = ismember(T{1}, known_ops);
if ~tf
error('IEF401H', 'Invalid operation', C{i});
end
result = op_handles{idx}(convert_operand(T{2}), convert_operand(T{3}));
Here, convert_operand() takes a string and does whatever is needed to return an operand value. In the example you give, your only possible operand is 0, but I am assuming that you will be wanting to use named variables or stack values or the like.
Cristian munoz
Cristian munoz 2013 年 5 月 24 日
編集済み: Cristian munoz 2013 年 5 月 24 日
Thanks Walter Roberson,
Best Regards

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by