How can I integrate a m.file consist of functions, input and output into MATLAB GUI

3 views (last 30 days)
Hi, I am doing a project with optimization concept whereby the end product is develop a GUI for it. As for starting level, i develop the functions using m.file whereby user will be asked for input and output will be shown. When convert the model GUI, I am having difficulties in assigning the functions into GUI. I got advised saying that call m.file from GUI to run the program. I would like to know how to assign value from edit box in GUI and pass the values to m.file, and display the result in GUI text box. Thanks in advance for your help.

Answers (1)

Alan Weiss
Alan Weiss on 28 Jul 2015
You should probably look at an example of a GUI and how the callback gets implemented. Here is a GUI that I wrote. If you don't care about Sudoku, you can just look at how the callbacks are implemented.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Arun Raj
Arun Raj on 28 Jul 2015
Edited: Arun Raj on 28 Jul 2015
Hi Alan, This is my m.file and GUI is as attached before.
function PV_OPTIMIZATION_HMS
% Smallest thickness of material available
d=0.0625;
% Linear inequality constraints
A=[-1 0 0.0193 0;0 -1 0.00954 0;0 0 0 1];
b=[0; 0; 240];
% Variable boundaries
Lb=[d; d; 10; 10];
Ub=[99*d; 99*d; 200; 200];
% Initial value
x0=Lb+(Ub-Lb).*rand(size(Lb));
% Creates an optimization options structure where the specified options
% (parameters) have specified values and unspecified options are set to
% []
options=optimset('Algorithm','sqp','Display','iter','TolFun'...
,1e-08);
% Returns the value of the objective function at the value x
[x,fval]=fmincon(@objfun,x0,A,b,[],[],Lb,Ub,@nonfun,options);
function f=objfun(x)
% Prompt that asking for input
prompt1 = 'What is the design volume[cubic inch]? ';
% User input
f = input(prompt1);
% Objective function
f = pi*(x(3)^2)*x(4)+(4/3)*pi*(x(3)^3);
% Nonlinear constraints
function [g,geq]=nonfun(x)
% Prompt that asking for input
prompt2 = 'What is the constraint volume[cubic inch]? ';
% User input
y = input(prompt2);
% Nonlinear inequality
g=pi*x(3)^2*x(4)+(4/3)*pi*x(3)^3-y;
% Equality constraint [none]
geq=[];
prompt3 = 'What is the Pressure [psi]? ';
p = input(prompt3);
prompt4 = 'What is the Allowable Stress[psi]? ';
s = input(prompt4);
prompt5 = 'What is the Weld Efficiency ? ';
e = input(prompt5);
prompt6 = 'What is the Corrosion Allowance[inch]? ';
c = input(prompt6);
% t1 = minimum required thickness at longitudinal seam welds
t1=(p*(x(3)-c))/(s*e-0.6*p);
% t2 = minimum required thickness at circular seam welds
t2=(p*(x(3)-c))/(2*s*e+0.4*p);
% tr = minimum required design thickness
tr=max(t1,t2)+c;
% p1 = longitudinal pressure applied to PV (shell)
p1=(s*e*(x(1)-c))/((x(3)-c)+0.6*(x(1)-c));
% p2 = circular pressure applied to PV (shell)
p2=(2*s*e*(x(1)-c))/((x(3)-c)-0.4*(x(1)-c));
% pr = maximum allowable pressure inside PV
pr=min(p1,p2);
% trh = minimum required thickness for hemispherical head
trh=(p*(x(3)-c))/(2*s*e-0.2*p)+c;
%Display the results (Variables)
disp('Following results are Thickness of Shell[inch],')
disp('Thickness of Head[inch],Radius of Shell[inch] and')
disp('Length of Shell[inch]')
disp (x)
disp('Minimum required Thickness for Shell[inch],')
disp (tr)
disp('Minimum required Thickness for Hemispherical Head[inch],')
disp (trh)
disp('Maximum allowable Pressure for Shell and Hemispherical Head[psi],')
disp (pr)
I can run this program and get a output. My aim is to link GUI with this m.file so that i can assign values from edit box of GUI, the values will be assigned to the variables as stated in input prompt (it will be disabled when linked to GUI) and the output will be displayed in GUI text box. In GUI, i tried to paste all the 3 functions to GUI file other than link it and i call the functions under calculate push button. Output is i can run the GUI and give input, but can't solve the problem as there are errors. Any comments or help in this matter. Thank you

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!