Display real time data in GUI.

23 次查看(过去 30 天)
Hi, I am trying display real time data in GUI,But unable to do it. My code is:
function varargout = voltage(varargin)
% VOLTAGE MATLAB code for voltage.fig
% VOLTAGE, by itself, creates a new VOLTAGE or raises the existing
% singleton*.
%
% H = VOLTAGE returns the handle to a new VOLTAGE or the handle to
% the existing singleton*.
%
% VOLTAGE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in VOLTAGE.M with the given input arguments.
%
% VOLTAGE('Property','Value',...) creates a new VOLTAGE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before voltage_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to voltage_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help voltage
% Last Modified by GUIDE v2.5 19-Nov-2019 09:28:29
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @voltage_OpeningFcn, ...
'gui_OutputFcn', @voltage_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before voltage is made visible.
function voltage_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to voltage (see VARARGIN)
% Choose default command line output for voltage
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes voltage wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = voltage_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
clear all
delete(instrfind)
global s
s=serial('COM7','BaudRate',9600,'Terminator','CR');
fopen(s);
% --- Executes on button press in voltage.
function START_Callback(hObject, eventdata, handles)
% hObject handle to voltage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
counter=1;
while 1
data=str2double(char(fread(s, 5).'));
data1=data/1000;
data2(counter)=data1
counter=counter+1;
plot(data2)
drawnow
end
And getting error like:
Undefined function or variable 's'.
Error in voltage>voltage_Callback (line 88)
data=str2double(char(fread(s, 5).'));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in voltage (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)voltage('voltage_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

采纳的回答

Walter Roberson
Walter Roberson 2019-11-19
Variables are only global inside of functions that declare them to be global. You can never force a variable to be global everywhere just by declaring it global in one place -- consider that if you could, then it would never be possible to write a secure bank account function because if hypothetically you could force variables to be global everywhere, then by using global with the names of the variables used in the bank account functions, you would have access to the secure variables in all other functions.
You need to declare the variable as global in the place it is used. Or better yet, http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
  3 个评论
Walter Roberson
Walter Roberson 2019-11-19
Look at animatedLine() as that is more efficient.
Walter Roberson
Walter Roberson 2019-11-20
I recommend explicitly specifying the data format expected on input and the data format expected for output when you use fread(), instead of hoping that the reader remember what the default is, and instead of hoping that you yourself will remember what the default is when you look back at the program in a few weeks. You are reading 5 items, but 5 what ? Bytes? Characters (which is not exactly the same) ? Double precision numbers?
If you are using str2double(char(fread))) then chances are that you should be using fscanf() instead, provided that there is a delimiter on the fields. If there is no delimiter, then you have to worry about how the other end knows that it is okay to go ahead and transmit the packet. You configured for Terminator CR, and CR works fine as a terminator and data seperator, so I suspect you can use fscanf(s, '%f', 1)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by