My global variables are disappearing!
6 views (last 30 days)
Show older comments
Apparently even my question are having errors. According to mathworks my question is less than four words long :(. I had to add this introduction so that it would work.
Hello again,
I posted a question ealier and have since managed to figure out the answer. The answer, however, just gave me an even stranger question.
First, the story. I have a GUI. It is a grumpy GUI. I declare a global variable called linkedObject at the initiation of the GUI. Later on I call this code:
function importButton_Callback(hObject, eventdata, handles)
global linkedObject;
fileName = get(handles.fileImpField, 'String');
delimStatus = get(handles.delimToggle, 'Value');
if delimStatus == 1
delim = get(handles.delimChar, 'String');
linkedObject.startDel(fileName, delim);
else
linkedObject.startRead(fileName);
end
set(handles.dispTable, 'Data', linkedObject.fullArray);
end
This code works fine. It gives me a shiny new array of imported data. It also pays attention to its global variable. When I have checked on the object at the end of the function it's properties are all as they should be (containing data).
The code that doesn't work is this:
function selColButton_Callback(hObject, eventdata, handles)
global linkedObject;
selectedColumn = linkedObject.getColumn(get(handles.selColField, 'Value'));
end
Apparently the list of columns suddenly no longer exists and my code tries to look in an empty variable for a cell array. After calling the first function these are the (important) listed properties.
Properties
selColumn: {1x18 cell}
fullArray: {1162277x18 cell}
After calling the second function (but before anything is actually done) these are the important listed properties.
Properties
selColumn: []
fullArray: []
In fact every property is simply [].
Also to note: The object's functions when used in the command window work just fine. It is when the gui gets involved that I have these issues.
Can anyone tell me whats going on? Any help is much appreciated :). Thank you.
Cheers,
John
0 Comments
Accepted Answer
Sebastian Holmqvist
on 12 Jul 2012
You could utilize e.g
setappdata(handles.gui_element, 'stored_var', data_var)
...
data_var = getappdata(handles.gui_element, 'stored_var')
Read about them and other approaches at http://www.mathworks.se/help/techdoc/creating_guis/f5-998352.html.
2 Comments
Sebastian Holmqvist
on 17 Jul 2012
Absolutely. But by accepting an answer you don't close the question. You merely state that the question is resolved and people can still find it.
More Answers (1)
Jan
on 12 Jul 2012
Edited: Jan
on 12 Jul 2012
The best idea is to avoid globals strictly. As you see, they are prone to errors and debugging the source of changes (or missing changes as in your case) is very complicated or near to impossible. So you run in exactly these problems global 's are famous for.
Therefore I dare not to post an analysis of your problem, but to suggest using a different approach.
2 Comments
See Also
Categories
Find more on Spreadsheets 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!