开发者

Problem displaying strings

Hey guyz. Can help me on this?

if size(cost,1) == 2

       A = (4*Pdt*cost(1,3)*cost(2,3)) + 2*(cost(1,2)*cost(2,3))+(cost(1,3)*cost(2,2));
       B = 2*(cost(2,3)+cost(1,3));
       lambda = num2str(A ./ B);
       set(handles.answer1_staticText,'String', lambda);
       P1 = (lambda - co开发者_StackOverflowst(1,2))./(2*cost(1,3));
       P2 = (lambda - cost(2,2))./(2*cost(2,3));
       PT = mat2str(P1 + P2);
       set(handles.answer2_staticText,'String', PT);
       guidata(hObject, handles);
end

From the coding above, the answer become like this :

[11.75 11.25 11.25 11.75 10.75 11.5 12.75 12.75 13]

My question is I want to display my answer at the static text box like this:

P1 = (%answer for P1)

P2 = (%answer for P2)

P TOTAL = (%answer for PT)

Can anyone help me with the coding?


You have converted lambda to a string (using num2str), and thus, the calculation of P1 etc will produce unexpected results.

It's better to only convert to string in the display step, so these accidents won't happen.

Try this:

if size(cost,1) == 2

       A = (4*Pdt*cost(1,3)*cost(2,3)) + 2*(cost(1,2)*cost(2,3))+(cost(1,3)*cost(2,2));
       B = 2*(cost(2,3)+cost(1,3));
       lambda = A ./ B;
       set(handles.answer1_staticText,'String', num2str(lambda));
       P1 = (lambda - cost(1,2))./(2*cost(1,3));
       P2 = (lambda - cost(2,2))./(2*cost(2,3));
       PT = P1 + P2;
       set(handles.answer2_staticText,'String', num2str(PT));
       guidata(hObject, handles);
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜