Variable evaluated with "evalin" vanishes

Hello everyone, I just received Matlab 2014b after working with 2012a and I tried to run my „old“ scripts. Now the problem I encountered was the following: I store some data x using assignin to the ‘base’ workspace. I created a GUI (with guide) which is supposed to (among others) plot the data x in an axis. This is the part of the code which should (and in 2012a DID) plot the data:
function Show_path_CreateFcn(hObject, eventdata, handles)
% hObject handle to Show_path (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
data_laser = evalin('base', 'ld');
plot(data_laser(:,3));
Now here is the strange part: It reads the data from the workspace (I know because when I leave out the semicolon, all numbers are “written out” in the command window) but the plot-function returns an error, telling me that my variable “data_laser” doesn’t exist. Can somebody help me here? Thank you in advance!

3 Comments

Can you provide the exact error message? Is it possible that the base workspace variable is being cleared?
Avoid using evalin and eval for passing variables like this. Use guidata or the handles structure instead and you will avoid this problem altogether.
Hello Rich Ohman,
This is the error message "Undefined function or variable "data_laser"". Or, as right now, there is no error message at all, it just doesn't plot anything :s How can I check if the variable is cleared from the 'base' workspace?

Sign in to comment.

 Accepted Answer

Thank you all for your help! :)
I found out why it’s not working. It is a problem (bug?) that I already encountered in older versions. As soon as I open the GUI with guide, the “tags” for the axes are gone. So when I open the property inspector, “ tag” is just empty. I have to go to the corresponding m-file, copy the name of the axis and insert it again as “tag”. Next I have to right-click the axis in guide, select View Callbacks and click “CreateFcn”. Only then the axis is “reconnected” to its create-function and then everything works out just fine :)
Have you encountered this problem before?

1 Comment

No. Never has happened for me. My tag properties have never vanished upon saving, closing, and reopening a fig file with GUIDE. What is the official bug number so I can look it up?

Sign in to comment.

More Answers (1)

OK, good. It's a blessing in disguise. Now you can program it up properly without using functions like evalin(), assignin(), and eval(). See the FAQ for better methods: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

4 Comments

Thank you for your fast reply Image Analyst!
But I still don't quite understand... I don’t want to share the data between different GUIs but between the GUI and the “main script” which does the data analysis. Also, if I understood correctly, assignin and evalin are not necessary but they should still work, right?
Probably. But are you sure that "ld" is still in the base workspace? How can you see the base workspace? When you're in the debugger you can only see the local workspace of the specific function that you're in.
What is the difference between your GUI and your main script? Is it all in one m-file that you're using GUIDE with? Or do you have a GUI m-file (either a script or a GUIDE-built function) and a separate "main script" m-file?
Yes, the GUI-m-File and the main script are two different m-files. The "main script" calls the GUI and waits until the GUI is closed again before it continues to process the data.
@Image Analyst, totally agree with using a better method of sharing data between gui and main code. I just want to correct something:
When you're in the debugger you can only see the local workspace of the specific function that you're in
When in the debugger, you can use dbup and dbdown to navigate between workspaces.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!