【问题标题】:Problem displaying strings显示字符串的问题
【发布时间】:2011-06-26 12:27:59
【问题描述】:

嘿家伙。可以帮帮我吗?

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)

谁能帮我写代码?

【问题讨论】:

  • 您需要确保最终结果是标量,而不是矩阵

标签: arrays user-interface matlab matrix


【解决方案1】:

您已将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

【讨论】:

  • 我怎样才能显示这样的答案:P1 = (%answer for P1) P2 = (%answer for P2) P TOTAL = (%answer for PT)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2020-04-26
相关资源
最近更新 更多