Global struct or separate globals for callback optimization?
If I needed a collection globals to store actively updated information, would it be better (run faster, run more efficiently, have less possibilities for bugs) to have a global struct in place of multiple globals? The various callbacks that update the information don’t use every single global, so a struct would bring along extra information in many of the places it is needed.
The motivation for this question is the UI I have designed, as globals are the only way that I currently understand how to enable interrupting callbacks to change the value of a variable that must be recognized by the interrupted callback. There is likely a better way to do this that doesn’t use globals, but my browsing didn’t reveal anything simple enough for my skill level and deadline. (please link alternatives to globals)!
Example:
%lines stores line graphics objects with 2 axes of 3-dimensional data
%each row in this array is an increment of the 3rd dimension
global lines; %is global for interrupting property changes of lines
lines = gobjects(80,4); %80 lines for each of 4 data sets
%Whether or not a playback iterating through each row of lines stops itself
global pauseState;
pauseState = true;
Where they would be replaced by the struct
global s;
s = struct(‘paused’,true,’lines’,gobjects(80,4))
Used by the callback functions
function pause()
global pauseState; %Here only one global is used, struct would be wasteful?
pauseState = true;
end
%While play() is running, pause() is called, interrupting and breaking the loop
function play()
global pauseState; %Here both globals are used, struct would be more compact
%global delay; %Another possible global
global lines;
%global lines2;
pauseState = false;
for i=1:1:height(lines)
if pauseState == true
break;
end
%do stuff
%[lines(i,:).Visibility] = deal(‘on’); %or something
%drawnow;
%pause(delay); %Dictate pacing of play loop
end
end
For the sake of simplicity, the operative code within play() is omitted since there would need to be more than placeholder graphics objects.
This example is not too far off from what a portion of my UI actually does, so these are some of the circumstances in which the struct substitution would be employed. I’m not sure of other cases.
The globals used here are of drastically different sizes, which I mention since the answer to this question may depend greatly on the size of the globals that would be replaced by the struct. Not all of the globals I am using are so polarized in size, like how "delay" and "pauseState" are a double and a logical value (both small).
Thank you!
Notes: I do not have any run errors or warnings, and I have not run into any issues with using multiple globals. I also use the clear; command at the top of my file which may be why I haven’t run into any issues with globals. The two callback functions here are used with uibutton objects.If I needed a collection globals to store actively updated information, would it be better (run faster, run more efficiently, have less possibilities for bugs) to have a global struct in place of multiple globals? The various callbacks that update the information don’t use every single global, so a struct would bring along extra information in many of the places it is needed.
The motivation for this question is the UI I have designed, as globals are the only way that I currently understand how to enable interrupting callbacks to change the value of a variable that must be recognized by the interrupted callback. There is likely a better way to do this that doesn’t use globals, but my browsing didn’t reveal anything simple enough for my skill level and deadline. (please link alternatives to globals)!
Example:
%lines stores line graphics objects with 2 axes of 3-dimensional data
%each row in this array is an increment of the 3rd dimension
global lines; %is global for interrupting property changes of lines
lines = gobjects(80,4); %80 lines for each of 4 data sets
%Whether or not a playback iterating through each row of lines stops itself
global pauseState;
pauseState = true;
Where they would be replaced by the struct
global s;
s = struct(‘paused’,true,’lines’,gobjects(80,4))
Used by the callback functions
function pause()
global pauseState; %Here only one global is used, struct would be wasteful?
pauseState = true;
end
%While play() is running, pause() is called, interrupting and breaking the loop
function play()
global pauseState; %Here both globals are used, struct would be more compact
%global delay; %Another possible global
global lines;
%global lines2;
pauseState = false;
for i=1:1:height(lines)
if pauseState == true
break;
end
%do stuff
%[lines(i,:).Visibility] = deal(‘on’); %or something
%drawnow;
%pause(delay); %Dictate pacing of play loop
end
end
For the sake of simplicity, the operative code within play() is omitted since there would need to be more than placeholder graphics objects.
This example is not too far off from what a portion of my UI actually does, so these are some of the circumstances in which the struct substitution would be employed. I’m not sure of other cases.
The globals used here are of drastically different sizes, which I mention since the answer to this question may depend greatly on the size of the globals that would be replaced by the struct. Not all of the globals I am using are so polarized in size, like how "delay" and "pauseState" are a double and a logical value (both small).
Thank you!
Notes: I do not have any run errors or warnings, and I have not run into any issues with using multiple globals. I also use the clear; command at the top of my file which may be why I haven’t run into any issues with globals. The two callback functions here are used with uibutton objects. If I needed a collection globals to store actively updated information, would it be better (run faster, run more efficiently, have less possibilities for bugs) to have a global struct in place of multiple globals? The various callbacks that update the information don’t use every single global, so a struct would bring along extra information in many of the places it is needed.
The motivation for this question is the UI I have designed, as globals are the only way that I currently understand how to enable interrupting callbacks to change the value of a variable that must be recognized by the interrupted callback. There is likely a better way to do this that doesn’t use globals, but my browsing didn’t reveal anything simple enough for my skill level and deadline. (please link alternatives to globals)!
Example:
%lines stores line graphics objects with 2 axes of 3-dimensional data
%each row in this array is an increment of the 3rd dimension
global lines; %is global for interrupting property changes of lines
lines = gobjects(80,4); %80 lines for each of 4 data sets
%Whether or not a playback iterating through each row of lines stops itself
global pauseState;
pauseState = true;
Where they would be replaced by the struct
global s;
s = struct(‘paused’,true,’lines’,gobjects(80,4))
Used by the callback functions
function pause()
global pauseState; %Here only one global is used, struct would be wasteful?
pauseState = true;
end
%While play() is running, pause() is called, interrupting and breaking the loop
function play()
global pauseState; %Here both globals are used, struct would be more compact
%global delay; %Another possible global
global lines;
%global lines2;
pauseState = false;
for i=1:1:height(lines)
if pauseState == true
break;
end
%do stuff
%[lines(i,:).Visibility] = deal(‘on’); %or something
%drawnow;
%pause(delay); %Dictate pacing of play loop
end
end
For the sake of simplicity, the operative code within play() is omitted since there would need to be more than placeholder graphics objects.
This example is not too far off from what a portion of my UI actually does, so these are some of the circumstances in which the struct substitution would be employed. I’m not sure of other cases.
The globals used here are of drastically different sizes, which I mention since the answer to this question may depend greatly on the size of the globals that would be replaced by the struct. Not all of the globals I am using are so polarized in size, like how "delay" and "pauseState" are a double and a logical value (both small).
Thank you!
Notes: I do not have any run errors or warnings, and I have not run into any issues with using multiple globals. I also use the clear; command at the top of my file which may be why I haven’t run into any issues with globals. The two callback functions here are used with uibutton objects. globals, callback, gui, struct, interruption MATLAB Answers — New Questions