MATLAB Answers

Display real time data in GUI.

34 views (last 30 days)
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)".
% 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});
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
gui_mainfcn(gui_State, varargin{:});
% 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
global 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)
while 1
data=str2double(char(fread(s, 5).'));
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)
Error in voltage (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in>@(hObject,eventdata)voltage('voltage_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Nov 2019
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,


MOHAN KHEDKAR on 19 Nov 2019
@Walter Roberson,thanks for your reply sir.I know the cocept of global variable.Firstly when i was trying to run this code without declaring 's ' as a global variable that time i was getting the same error like 'Undefined function or variable 's'.So, for that i was truing to declare it as a global,just for trial error.But what about my remaining code? As i am new to this environment,don't know what's going on.Please give me any suggestion.
Walter Roberson
Walter Roberson on 19 Nov 2019
Look at animatedLine() as that is more efficient.
Walter Roberson
Walter Roberson on 20 Nov 2019
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)

Sign in to comment.

More Answers (0)

Translated by