Kode2 yg umum dlm pembuatan Matlab GUI

Kode2 ini ditulis di m-file setelah GUI dibuat di Matlab GUI, bukan di m-file! All codes for programming the GUI have to be written after the callback function, except for putting toolbar on the figure. The following code should be written before the guidata in the opening function.
set(hObject,’toolbar’,’figure’);

After all codes written, it is recommended to write the following code to update the data:
guidata(hObject,handles);

Other useful programming codes are:
– store value of input edit text as string. If the string is not a number then input will be empty:
input=str2num(get(hObject,’String’));

– check to see if the input is empty. If so, default input edit text to zero:
if(iesempty(input))
set(hObject,’String’,’0’)
end

– get value from slider:
slidervalue=get(handles.slider,’Value’);

– put the value to edit text:
set(handles.slider_edittext,’String’,num2str(slidervalue));

– get value from edit text:
slidervalue=get(handles.slider_edittext,’String’);

– convert the value into number:
slidervalue=str2num(slidervalue);

– if user inputs something that is not a number, or if it is less than 0 (lower limit) or greater than 100 (higher limit), then the slidervalue defaults to 0:
if(isempty(slidervalue)║slidervalue100
set(handles.slider1,’Value’,0);
set(handles.slider_edittext,’String’,’0’);
else
set(handles.slider1,’Value’,slidervalue);
end

Where, slider1 is the slider tag name.

– popup callback:
switch get(handles.popupmenu1,’Value’)
case 1
return
case 2
set(handles.edittext,’FontSize’,8);
case 3
set(handles.edittext,’FontSize’,10);
otherwise
end

– to introduce variables that are going to be used in other callbacks, use:
handles.variable_name = XXX;

– select axes:
axes(handles.axes1)

– plot, only if we already select the axes:
x=…
y=…
plot(x,y);
title(‘axes1’);
xlabel(‘x data’);
ylabel(‘y data’);
title(‘title’);

– to clear the axes:
cla(handles.axes1,’reset’);

– before doing simulink simulation, we have to configure the options because we have to use current workspace to simulate the model:
options=simset(‘SrcWorkspace’,’current’);

– then we simulate:
sim(‘simulinkmodelname’,[],options);

– call other gui:
gui’sname();

– close the gui:
close(gui’sname);

– upload data from excel to be used in gui (type in the edit text the name of the Excel file):
input=get(handles.edittext,’String’);
Data=xlsread(input);

– import figure on the axis
In the opening function, type this code:
background=importdata(‘filename.jpg’);
axes(handles.axes1);
image(background);
axis off

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: