Effacer les filtres
Effacer les filtres

My global variables are disappearing!

2 vues (au cours des 30 derniers jours)
John Callahan
John Callahan le 12 Juil 2012
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

Réponse acceptée

Sebastian Holmqvist
Sebastian Holmqvist le 12 Juil 2012
You could utilize e.g
setappdata(handles.gui_element, 'stored_var', data_var)
...
data_var = getappdata(handles.gui_element, 'stored_var')
  2 commentaires
John Callahan
John Callahan le 12 Juil 2012
Thank you. This is what I ended up going with. I will leave the question up though. That way if someone finds an answer then someone else may be able to benefit from it.
Sebastian Holmqvist
Sebastian Holmqvist le 17 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 12 Juil 2012
Modifié(e) : Jan le 12 Juil 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 commentaires
John Callahan
John Callahan le 12 Juil 2012
Modifié(e) : John Callahan le 12 Juil 2012
Thank you for your insight. Is there another way to get all of my gui functions to reference the same object without passing it in as an argument?
Jan
Jan le 12 Juil 2012
See: set(FigureHandle, 'UserData'), set/getappdata, guidata.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by