How do I make gui code for existing matlab program

13 views (last 30 days)
Hello all,
I have written a code below where I will get values from serial port and using that I am plotting a graph. But I need it in GUI, I am new to matlab, so I don't know much about it. I saw few videos but still I didn't get it done. Please help me on this.
Thanks in advance.
% Create a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM3', 'BaudRate',9600,'Terminator','LF');
%obj1.BaudRate=9600
else
fclose(obj1);
obj1 = obj1(1);
end
% Connect to instrument object, obj1.
fopen(obj1);
% Communicating with instrument object, obj1.
Data1 = [1:10];
Data2 = [1:10];
Data3= [1:10];
Data4= [1:10];
for i=1:10
Data1(i) = str2double(query(obj1, '*IDN?'));
Data2(i) = str2double(query(obj1, '*IDN?'));
Data3(i)= str2double(query(obj1, '*IDN?'));
Data4(i)=str2double(query(obj1, '*IDN?'));
end
x = [Data2];
y =[Data1];
a =[Data3];
b=[Data4];
h = linspace(-pi,pi);
y1 = sin(h);
hold on
plot(x,y);
plot(h,y1)
plot(a,b);
%axis([-50 50 -50 50])
hold off
fclose(obj1);
% Clean up all objects.
delete(obj1);
  2 Comments
Adam Danz
Adam Danz on 5 May 2019
Edited: Adam Danz on 5 May 2019
There are already demos you can follow provided by the Matlab documentation. If you have any troubles getting started or if you get stuck, follow-up with a specific question and I'd be glad to help.
Rik
Rik on 6 May 2019
I would strongly advise not using GUIDE to build your GUI. I have no experience with App Designer, so I have no opinion on that one, but GUIDE results in bloated, hard to read, hard to maintain, hard to port code.
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.

Sign in to comment.

Answers (0)

Categories

Find more on Update figure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!