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 - cost(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
从上面的编码中,答案变成这样:
[11.75 11.25 11.25 11.75 10.75 11.5 12.75 12.75 13]
我的问题是我想在静态文本框中显示我的答案,如下所示:
P1 = (%answer for P1)
P2 = (%answer for P2)
P TOTAL = (%answer for PT)
任何人都可以帮我编码吗?
答案 0 :(得分:0)
您已将lambda
转换为字符串(使用num2str
),因此P1
等的计算会产生意外结果。
最好只在显示步骤中转换为字符串,这样就不会发生这些事故。
试试这个:
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