How to display values with formatting, converting numbers to strings.

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
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?

 Risposta accettata

You can build the multiline text, for example:
{['P1 =' string1],['P2 =' string2],['P3 =' string3]}

Più risposte (8)

If I interpret your question correctly, you're asking how to get the nicely formatted output in the text box. If that's the case, use sprintf instead of mat2str to define the string PT before using set.
@paulo silva can you show the full coding please?
@matt sorry if my question hardly to understand. Can you show me the coding using sprintf? Because i'm quite new with matlab

3 Commenti

string1=mat2str(P1);
string2=mat2str(P2);
string3=mat2str(P1+P2);
set(handles.answer2_staticText,'String', {['P1 =' string1],['P2 =' string2],['PT =' string3]});
Similar code with sprintf:
PTM=sprintf('P1=%s\nP2=%s\nPT=%s',mat2str(P1),mat2str(P2),mat2str(P1+P2));
set(handles.answer2_staticText,'String', PTM);
No worries, I just wanted to make sure I was answering the right question! Paulo's response is what I was thinking - the key being the use of \n to get line breaks. (Thx Paulo)
BTW, another way to get multiple lines is the use char(10) to print a line break: txt = ['string1',char(10),'string2']

Accedi per commentare.

@paulo: I have tried your coding but it seems there is an error like this :
Warning: Single line Edit Controls can not have multi-line text > In gui_aku2>Mybutton_Callback at 105
In gui_mainfcn at 96
In gui_aku2 at 42
In @(hObject,eventdata)gui_aku2('Mybutton_Callback',hObject,eventdata,guidata(hObject))
And my edit text box is missing after i push the button to run the program. Can you tell me what is the problem anyway?
@matt: what would be the answer for your example??
function multilinetext
hf = figure;
uicontrol(hf,'style','pushbutton','string','update',...
'position',[100,100,60,20],'callback',@updatetxt);
hin = uicontrol(hf,'style','edit','position',[100,140,200,20]);
hout = uicontrol(hf,'style','text','position',[100,180,200,60]);
function updatetxt(~,~,~)
txt = get(hin,'string');
x = str2num(txt);
y = 2*x;
strng = ['x = ',txt,char(10),'y = ',mat2str(y),...
char(10),'sum = ',mat2str(x+y)];
set(hout,'string',strng)
end
end

3 Commenti

Or, if you prefer:
strng = sprintf('x = %s\ny = %s\nz = %s',txt,mat2str(y),mat2str(x+y));
@matt: where should i put this code??
this was just a standalone function to show how to use char(10). without your full code, I can't really test an answer to be sure that it will work, but I'll give it a go...

Accedi per commentare.

@paulo : there was an error at this code. This is my code that i paste from your coding:
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));
string1=mat2str(P1);
string2=mat2str(P2);
string3=mat2str(P1+P2);
set(handles.answer2_staticText,'String', {['P1 =' string1],['P2 =' string2],['PT =' string3]});
PTM=sprintf('P1=%s\nP2=%s\nPT=%s',mat2str(P1),mat2str(P2),mat2str(P1+P2));
set(handles.answer2_staticText,'String', PTM);
So there is an error at:
set(handles.answer2_staticText,'String', {['P1 =' string1],['P2 =' string2],['PT =' string3]});
it says that string1 does not define??

3 Commenti

I tried the code and it's working fine here.
can u paste your code here?? Coz i tried the code above here but it got error.. hurmmmm
I used the exactly same code but had to use other values because I have no idea what's that Pdt and cost, I just commented the first lines and used P1=[1 2 3];P2=[4 5 6]

Accedi per commentare.

@paulo: well i edit the code be like this:
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);
lambda1 = num2str(A ./ B);
set(handles.answer1_staticText,'String', lambda1);
P1 = (lambda - (cost(1,2)))./(2*cost(1,3));
P2 = (lambda - cost(2,2))./(2*cost(2,3));
string1=mat2str(P1);
string2=mat2str(P2);
string3=mat2str(P1+P2);
set(handles.answer2_staticText,'String', {['P1 =' string1],['P2 =' string2],['PT =' string3]});
PTM=sprintf('P1=%s\nP2=%s\nPT=%s',mat2str(P1),mat2str(P2),mat2str(P1+P2));
set(handles.answer2_staticText,'String', PTM);
now the error become like this :
Warning: Single line Edit Controls can not have multi-line text
What does this mean??

1 Commento

I believe the problem is related to the type of object, if you have edit text boxes you might have to change the Max value property, this is from the Matlab Help:
If the edit text Max and Min properties are set such that Max - Min > 1, the user can enter multiple lines. For example, setting Max to 2, with the default value of 0 for Min, enables users to enter multiple lines.

Accedi per commentare.

Try this (as a cut/paste replacement of the code you posted):
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));
string1=mat2str(P1);
string2=mat2str(P2);
string3=mat2str(P1+P2);
set(handles.answer2_staticText,'String',...
['P1 = ',string1,char(10),...
'P2 = ',string2,char(10),'PT = ',string3]);
guidata(hObject, handles);
end

9 Commenti

@matt:
i have tried the coding you gave me and its still error.. or should i also add this code under guidata(hObject, handles);??
PTM=sprintf('P1=%s\nP2=%s\nPT=%s',mat2str(P1),mat2str(P2),mat2str(P1+P2));
set(handles.answer2_staticText,'String', PTM);
well i can display the value of P1,P2 and PT at the command window but it seems i cant display the answer at the edit text box figure. I dont know what is the problem. it just say Warning: Single line Edit Controls can not have multi-line text
see my answer above, you might need to change the Max property of answer2_staticText, do this before your code:
set(handles.answer2_staticText,'Max',2)
but if i do the codine like this:
PT = num2str(P1 + P2);
set(handles.answer2_staticText,'String',PT);
guidata(hObject, handles);
i can display the answer at the edit text box but only PT. How can i make it display P1, P2 and PT??
@paulo: now it works!!!! i change the max property for the answer.. now it can display the answer very well.. THX!!!
@paulo: another thing is my answer will display up to 10 decimal places.. How to make it to 4 decimal places only??
doc mat2str
you will find that you can add another argument to mat2str, it's the precision, example:
mat2str(P1,4)
now its working.. thx =)
ah, i think i finally understand what's going on here. answer2_staticText isn't actually a static text uicontrol -- it's an editable text uicontrol, right? uicontrol of style 'text' doesn't have the multiline restriction; style 'edit' does.

Accedi per commentare.

@paulo and @matt: THX A LOT GUYZ!!! YOU BOTH REALY HELP ME HERE

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by