Global variables not working

16 views (last 30 days)
Alexander MacDonald
Alexander MacDonald on 7 May 2021
Answered: Asvin Kumar on 10 May 2021
Hi All,
I'm working on a program that will interface with multiple pieces of serial hardware. For this I will obviously need to be able to access the serial port for read/write purposes. I decided to store the objects in a global array, but it just isn't working. Here's my function which connects to the devices;
*Note that the connect function is assigned to a callback for a button on the app.
function s = connect(port, index, app)
if isempty(port)
msgbox('Select a valid COM port');
return;
end
try
s = serialport(port, 9600);
catch
msgbox('Unable to connect to COM port');
return;
end
serialObj(index,1,s);
end
%if io = 0, that denotes a get
%if io = 1, that denotes a set
function s = serialObj(index, io, vargin)
global serialObject
disp(length(serialObject));
switch io
case 0
s = serialObject(index);
case 1
% try
% serialObject(index) = vargin(1);
% catch
clear serialObject;
serialObject(index) = vargin(1);
disp(length(serialObject));
s = vargin(1);
% end
end
end
I also have a function that deletes the serialObject, but I was getting an error saying that the value at the array index was null. The first disp(lenght(serialObject)) is alway 0, whereas the second disp(length(serialObject)) is always 1. The first one should be 1 if I've already pressed be button, but the value does not seem to be held in the serialObject variable.
Does anyone know what is going on here?
Thanks!

Answers (1)

Asvin Kumar
Asvin Kumar on 10 May 2021
Your global variable 'serialObject' has no data in it. That's the root cause. We'll see why.
When the code hits the first disp statement, it displays 0 because there's no data in the global variable. By the time the code hits the second disp statement, you have deleted the local copy of the global variable 'serialObject'. So, the serialObject(index) = vargin(1); command creates a new local variable named 'serialObject'. And the disp statement displays the length of that local variable. This length depends on the input argument 'index' which in your case is 1.
clear only clears local copies of variables unless explicitly specified. You can read more about clearing global variables in the hyperlink.
In essence, your global variable 'serialObject' never gets assigned a value because of the clear command.
Also, it is generally not considered a good practice to use globals. Why it's not a good practice has been discussed several times on this forum. Here's one thread. You can search for more discussions.
Consider the following alternatives to have cleaner and safer code.
1. Pass serialObject as input and return it as an output once it is updated.
function [s, serialObject] = serialObj(serialObject, index, io, vargin)
% your code in here
end
2. Create serialObject variable inside the connect function and nest the serialObj function inside the connect function so that the serialObject variable is available to all calls of the serialObj function.
function s = connect(port, index, app)
serialObject = [];
% ... your code here
function s = serialObj(index, io, vargin)
% use serialObject without global now
% ... your code here
end
end

Community Treasure Hunt

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

Start Hunting!