I wrote a code which is a pattern of popupmenu and edit and static Iu control but it does not work, would you help me?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Elmira Jafari
am 18 Feb. 2021
Kommentiert: Cris LaPierre
am 19 Feb. 2021
function test
e = uicontrol('Style','Edit','Units','Normalized','Position',[.4 .5 .2 .1]);
uicontrol('Style','text','Units','Normalized',...
'Position',[.2 .45 .2 .1],'String','Number of laminas');
uicontrol('Style','PushButton','Units','Normalized',...
'Position',[.4 .3 .2 .1],'String','Create','Callback',@b_clbck);
function b_clbck(hObject, eventdata, handles)
n = str2double(get(e,'String'));
create_figure(n)
end
function create_figure(n)
figure('Units','Normalize','Name','Mechanical Properties')
for k=1:n
uicontrol('Style','Edit','Units','Normalized',...
'Position',[.1 k/n-.75/n .1 .75/n],'String',sprintf('Lamina %#d',k));
uicontrol('Style','popupmenu','String',{'Graphite','Boron'},'Units','Normalized',...
'Position',[.25 k/n-.75/n .1 .75/n],'Callback',@c_clbck);
uicontrol('Style','Edit','Units','Normalized',...
'Position',[.38 k/n-.75/n .08 .75/n],'Callback',@d_clbck);
uicontrol('Style','text','Units','Normalized',...
'Position',[.35 (k/n)-.75/n .03 .75/n],'String','Qx=');
uicontrol('Style','pushbutton','Units','Normalized',...
'Position',[1 k/n-.75/n .08 .75/n],'Callback',@e_clbck);
end
function c_clbck(hObject, eventdata, handles)%popupmenu
idx=get(handles.c,'Value');
switch idx
case 1
S.Ex=7;
S.Ey=20;
case 2
S.Ex=5;
S.Ey=30;
otherwise
end
set(handles.c, 'UserData', S);
end
function d_clbck(hObject, eventdata, handles)%Qx
end
function e_clbck(hObject, eventdata, handles)%pushbutton
S = get(handles.c, 'UserData')
Ex=S.Ex;
Ey=S.Ey;
Qx=Ey/Ex;
set(handles.d,'string',Qx)
end
end
end
12 Kommentare
Rik
am 18 Feb. 2021
In addition to what Cris told you: for general advice and examples for how to create a GUI have look at this thread.
Akzeptierte Antwort
Cris LaPierre
am 18 Feb. 2021
Bearbeitet: Cris LaPierre
am 18 Feb. 2021
Some issues to take into consideration.
You appear to be trying to implement the nested function approach. I would suggest splitting your code into two main functions, one for each figure window.
Another issue is that you are not capturing your component handles. You will need these to tell your callbacks which edit field to update, for example. Because the number of components will depend on the value the user enters, your handles will have to be captured in arrays.
While hObject will let you access the properties of the calling object, you will need a way to refer to other components on the same row. I suggest adding a Tag property that contains the value of your loop index, k. You can query the tag of the invoking component to let you know what handle in the array to reference.
I would not have your popup callback set the value of Ex and Ey. Instead, I would set these values as part of the Calculate callback. I would use your popup callback to clear the string value of the edit field as a flag that the result needs to be recalculated.
You can also use dot notation instead of get and set.
n = str2double(e.String);
6 Kommentare
Rik
am 19 Feb. 2021
@Cris LaPierre interesting how similar that is to what I suggested in the other thread. I guess my practice is close enough to common practice to have it in common with you ;)
Cris LaPierre
am 19 Feb. 2021
I'm glad to hear it's not wildly off base. I don't create apps programmatically, so I tried to stick pretty close to the nested function example. Perhaps we both learned from the same place - the doc!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

