how to save each array's results in dependent blank in GUI program base code?

2 次查看(过去 30 天)
I have some arrays that would change their number by user inputs.
now assume 24 arrays we have, for each one , I have 2 results by pushing calculate button.
the result of all arrays are like the first one. while I expect the results of others change by changing its value of its pop up menu.
I dont know how give tag or index in program base GUI matlab?
other question is how could I revese the y direction , as you can see from pic my lamina 24 is on top of paper while lamina1 is at the end.
function laminate
f1 = figure('Name','GUI','Menubar','none','Unit','normal','Position',[0.2 0.3 0.5 0.5])
e = uicontrol('Style','Edit','Units','Normalized','Position',[.4 .5 .2 .1]);
l = uicontrol('Style','text','Units','Normalized','Position',[.2 .45 .2 .1],'String','Number of laminas');
m = 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)
f2 = figure('Name','Mechanical properties','Menubar','none','Unit','normal','Position',[0.1 0.1 0.85 0.85]);
up = uipanel('Title','laminates','Unit','normal','Position',[0.05 0.05 0.9 0.9]);
for k=1:n;
lam = uicontrol('Style','Edit','Units','Normalized','Position',[0.01 k/n-.75/n .1 .75/n],'String',sprintf('Lamina %#d',k),'parent',up);
pop = uicontrol('Style','popupmenu','String',{'Graphite','Boron'},'Units','Normalized','Position',[.1 k/n-.75/n .1 .75/n],'Callback',{@popupmenu_callback},'parent',up);
con1 = uicontrol('Style','text','Units','Normalized','Position',[.2 (k/n)-.75/n .03 .75/n],'String','Qx=','parent',up)
res1 = uicontrol('Style','Edit','Units','Normalized', 'Position',[.22 k/n-.75/n .05 .75/n],'Callback',{@ans_callback},'parent',up);
con2 = uicontrol('Style','text','Units','Normalized','Position',[.27 (k/n)-.75/n .03 .75/n],'String','Qy=','parent',up)
res2 = uicontrol('Style','Edit','Units','Normalized', 'Position',[.30 k/n-.75/n .05 .75/n],'Callback',{@ans_callback},'parent',up);
Cal = uicontrol('Style','pushbutton','Units','Normalized','Position',[0.7 k/n-.75/n .08 .75/n],'string','Calculate','Callback',{@Calc_callback},'parent',up);
end
function popupmenu_callback(hObject,eventdata)
for k=1:n;
idx = get(hObject,'value');
setappdata(0,'evalue',idx);
end
end
function Calc_callback(hObject,eventdata)%pushbutton
for k=1:n;
idx = getappdata(0,'evalue');
switch idx;
case 1 % User selects Peaks.
ex=5;
ey=20;
Qx=ey/ex;
Qy=ey*ex;
set(res1,'string',Qx)
set(res2,'string',Qy)
case 2 % User selects Membrane.
ex=5;
ey=30;
Qx=ey/ex;
Qy=ey*ex;
set(res1,'string',Qx)
set(res2,'string',Qy)
otherwise
end
end
end
end
end
  3 个评论
Rik
Rik 2021-2-19
From your edit I get the feeling you are not the original author of this code. Did you contact the original author? You should keep your usage in the back of your mind when writting the code that generates fields with callbacks. Your code has several loops where data is getting overwritten and you do not seem to use object tags at all.
Elmira Jafari
Elmira Jafari 2021-2-19
I have written it by myself and help of internet and videos. but now I am reallt struggling with it as , I dont know how to stop this overwrittening. I dont know how to use tags in program base GUI.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2021-2-19
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread. Below I show you some edits to make your code more parametric and readable.
lamina_handles=struct('lam',cell(0,n));
for k=1:n
vert_pos=[k/n-0.75/n 0.75/n];
lamina_handles(k).lam = uicontrol('Style','Edit','Parent',up,...
'Units','Normalized','Position',[0.01 vert_pos(1) .1 vert_pos(2)],...
'String',sprintf('Lamina %#d',k));
lamina_handles(k).pop = uicontrol('Style','popupmenu','Parent',up,...
'String',{'Graphite','Boron'},...
'Units','Normalized','Position',[0.10 vert_pos(1) 0.10 vert_pos(2)],...
'Callback',@popupmenu_callback,...
'Tag',num2str(k));
lamina_handles(k).con1 = uicontrol('Style','text','Parent',up,...
'Units','Normalized','Position',[0.20 vert_pos(1) 0.03 vert_pos(2)],...
'String','Qx=');
lamina_handles(k).res1 = uicontrol('Style','Edit','Parent',up,...
'Units','Normalized','Position',[0.22 vert_pos(1) 0.05 vert_pos(2)],...
'Callback',@ans_callback,...
'Tag',num2str(k));
lamina_handles(k).con2 = uicontrol('Style','text','Parent',up,...
'Units','Normalized','Position',[0.27 vert_pos(1) 0.03 vert_pos(2)],...
'String','Qy=');
lamina_handles(k).res2 = uicontrol('Style','Edit','Parent',up,...
'Units','Normalized','Position',[0.30 vert_pos(1) 0.05 vert_pos(2)],...
'Callback',@ans_callback,...
'Tag',num2str(k));
lamina_handles(k).Cal = uicontrol('Style','pushbutton','Parent',up,...
'Units','Normalized','Position',[0.70 vert_pos(1) 0.08 vert_pos(2)],...
'String','Calculate',...
'Callback',@Calc_callback,...
'Tag',num2str(k));
end
With this layout it is easy to see all properties of your uicontrol objects. I prefer to put the style and parent on the first line, put Units and Position on the same line, and to write the rest of the properties each on their own line.
I also changed the positions parameter to a variable, so you can adapt it in one place. That will allow you to flip the direction.
Every object with a callback now also has the Tag property set. That means you can do this in the callback function to retrieve all objects on that line:
k=str2double(get(gcbo,'Tag'));
h=lamina_handles(k);
  10 个评论
Rik
Rik 2021-2-21
If you want to have the same material selected, why provide the dropdown menu again? You might also consider closing the first figure once the user has entered the number of lamina.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by