Sum two values coming from eval

Hi,
I would like to ask how to sum two numerical values, which coming from the eval fucntion
set(handles.A_edit,'string',eval(char(strcat('handles.forces.',gridnames(gridnum,1),'.total.B_IBE'))));
+
set(handles.A_edit,'string',eval(char(strcat('handles.forces.',gridnames(gridnum,1),'.total.C_IBE'))));

3 个评论

why you have used eval ???
Hi KSSV,
just picked up an existing code that used this eval function.
I have just to sum two force component.
Regards
"just picked up an existing code that used this eval function."
For more than ten years it has been recommended to avoid using eval to dynamically access structure fieldnames, since MATLAB 6.5:
So whoever wrote that code is badly informed and uses poor programming practices. Do NOT learn from that code!

请先登录,再进行评论。

 采纳的回答

Plenty of problems with your code.
For a start set as you use it does not return a value, so there is nothing to sum. I'm going to assume that's the output of eval you want to sum. Nothing is stopping you to sum these output before you call set.
2nd problem: Can the B_IDE ad C_IBE be summed? For that they would need to be numeric. But the string property of uicontrol expects text. Perhaps the sum should be converted to text, but you don't show that.
3rd problem: I don't see the point of the char. strcat already outputs a char array. Unless gridnames is a cell array of course, but in that case, clearly you don't know how to extract values from a cell array (using {}). In that case,
char(strcat('handles.forces.',gridnames(gridnum,1),'.total.B_IBE'))
should be:
strcat('handles.forces.',gridnames{gridnum,1},'.total.B_IBE') %and if gridnames is a vector cell array 2d indexing is a waste of time.
And of course, 4th problem is the use of eval. It's completely unnecessary as well here as there's much simpler syntax to obtain the same.
In the end, I'm going to guess that what you want to do is:
B_total = handles.forces.(gridnames{gridnum}).total.B_ICE;
C_total = handles.forces.(gridnames{gridnum}).total.C_ICE;
set(handles.A_edit, 'string', num2str(B_total + C_total))

4 个评论

Davide Di Pasquale's "Answer" moved here and formatted properly:
Stil problem:
I have modified the code following Guillaume
B_total = strcat(handles.forces.(gridnames{gridnum,1}).total.VISCOUSDRAG_TE);
C_total = strcat(handles.forces.(gridnames{gridnum,1}).total.CD_vortd);
set(handles.cd_edit, 'string', num2str(B_total + C_total));
But I got this error:
Undefined operator '+' for input arguments of type 'cell'.
Error in vfp_post1>options_Callback (line 432)
set(handles.cd_edit, 'string', num2str(B_total + C_total));
Anyone can help?
Thanks in advance
Regards
Again, what you write doesn't make sense.
B_total = strcat(...
C_total = strcat(...
So B_total and C_total are guaranteed to be either char arrays or cell arrays of char arrays (or strings). You then attempt to add them as if they were numbers. Of course, matlab is not going to like that.
So:
a) explain exactly what you're trying to do.
b) Tell us what is the output of
class(handles.forces.(gridnames{gridnum,1}).total)
size(handles.forces.(gridnames{gridnum,1}).total)
class(handles.forces.(gridnames{gridnum,1}).total(1).VISCOUSDRAG_TE)
size(handles.forces.(gridnames{gridnum,1}).total(1).VISCOUSDRAG_TE)
Davide Di Pasquale's "Answer" moved here:
Hi Guillaume,
Now the problem is solved:
B_total = strcat(handles.forces.(gridnames{gridnum}).total.VISCOUSDRAG_TE);
C_total = strcat(handles.forces.(gridnames{gridnum}).total.CD_vortd);
set(handles.cd_edit, 'string', num2str(str2double(B_total)+ str2double(C_total)));
Thanks
@Davide Di Pasquale: I accepted Guillaume's answer on your behalf, as you indicated that this answer helped you to resolve your question.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

产品

版本

R2017a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by