MATLAB Answers

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

41 ビュー (過去 30 日間)
Elmira Jafari
Elmira Jafari 2021 年 2 月 19 日
コメント済み: Elmira Jafari 2021 年 2 月 21 日
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 件のコメント
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 件のコメント

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by