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?

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 commentaires

Please provide more details on what you mean by "it doesn't work". What is it supposed to do and why is the current behavior not correct?
Dear Cris,
firstly my code should ask howmany laminas do you have, then I put forexample 24. then it should make a sth like that picture that I sent . after I pick Graphit or Boron. it should give me the Qx. but when I push calculate it gives me an error
Share everything we would need to reproduce the error. You can use the paperclip icon to attach files to your post.
Part of the issue appears to be your callback function inputs. When you create your app programmatically, you only have 2 inputs, not 3. See this page. Because your callbacks have all been defined with 3, the following error:
Not enough input arguments.
Error in LiveEditorEvaluationHelperE1134043204>test/create_figure/c_clbck (line 32)
idx=get(handles.c,'Value');
Error while evaluating UIControl Callback.
Your inputs should be (src,events)
thank you,
does src.popupmenu does the same work as handles.popupmenu?
how should I get value of a function?
befor I wrote idx=get(handles.c,'Value');
now what can I write?
please help me
In addition to what Cris told you: for general advice and examples for how to create a GUI have look at this thread.

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

would you please give me an example? I didnt get it
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)
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',{@popupmenu_callback});
uicontrol('Style','text','Units','Normalized',...
'Position',[.35 (k/n)-.75/n .03 .75/n],'String','Qx=');
uicontrol('Style','Edit','Units','Normalized',...
'Position',[.38 k/n-.75/n .08 .75/n],'Callback',{@ans_callback});
uicontrol('Style','pushbutton','Units','Normalized',...
'Position',[0.5 k/n-.75/n .08 .75/n],'string','Calculate','Callback',{@Calc_callback});
end
end
function ans_callback(hObject,eventdata)%Qx
end
function popupmenu_callback(hObject,eventdata)
idx = get(hObject,'value');
setappdata(0,'evalue',idx);
end
function Calc_callback(hObject,eventdata)%pushbutton
idx = getappdata(0,'evalue');
switch idx;
case 1 % User selects Peaks.
ex=7;
ey=20;
Qx=ey/ex;
case 2 % User selects Membrane.
ex=5;
ey=30;
Qx=ey/ex;
otherwise
end
%????????Idont know how to put Qx which is the final result in the string of ans
%???? and how to put a loop for inserting final result in each instring of ans
end
end
If you are going to do nested functions, you do not need to use application data (set/getappdata).
You need to capture your uicontrol handles (at least the ones you are going to refer back to). Here's an example taken from the nest function section of the documentation page I linked ot earlier.
button = uicontrol('Parent', hfig,'Style','pushbutton',...
'Units','normalized',...
'Position',[0.4 0.3 0.2 0.1],...
'String','Display Difference',...
'Callback',@button_callback);
Then I can use button inside another callback to refer to that component.
Here's what my approach might look like.
function test
hfig = figure();
e = uicontrol('Parent', hfig,'Style','Edit','Units','Normalized','Position',[.4 .5 .2 .1]);
uicontrol('Parent', hfig,'Style','text','Units','Normalized',...
'Position',[.2 .45 .2 .1],'String','Number of laminas');
uicontrol('Parent', hfig,'Style','PushButton','Units','Normalized',...
'Position',[.4 .3 .2 .1],'String','Create','Callback',@b_clbck);
function b_clbck(hObject, eventdata)
n = str2double(e.String);
create_figure(n)
end
end
function create_figure(n)
hfig1 = figure('Units','Normalize','Name','Mechanical Properties');
for k=1:n
uicontrol('Parent', hfig1,'Style','Edit','Units','Normalized',...
'Position',[.1 k/n-.75/n .1 .75/n],'String',sprintf('Lamina %#d',k));
handles.pop(k) = uicontrol('Parent', hfig1,'Style','popupmenu','String',{'Graphite','Boron'},'Units','Normalized',...
'Position',[.25 k/n-.75/n .1 .75/n],'Tag',num2str(k),'Callback',@c_clbck);
uicontrol('Parent', hfig1,'Style','text','Units','Normalized',...
'Position',[.35 (k/n)-.75/n .03 .75/n],'String','Qx=');
handles.QxVal(k) = uicontrol('Parent', hfig1,'Style','Edit','Units','Normalized',...
'Position',[.38 k/n-.75/n .08 .75/n]);
uicontrol('Parent', hfig1,'Style','pushbutton','Units','Normalized','String','Calculate',...
'Position',[.53 k/n-.75/n .1 .75/n],'Tag',num2str(k),'Callback',@e_clbck);
end
function c_clbck(hObject, eventdata)
r = str2double(hObject.Tag);
handles.QxVal(r).String = "";
end
function e_clbck(hObject, eventdata)%pushbutton
r = str2double(hObject.Tag);
switch handles.pop(r).Value
case 1
Ex=7;
Ey=20;
case 2
Ex=5;
Ey=30;
end
Qx = Ey/Ex;
handles.QxVal(r).String = num2str(Qx);
end
end
@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 ;)
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by