Why does GETAPPDATA give errors in subsref when the value-name has spaces or special characters?

1 view (last 30 days)
Why does GETAPPDATA give errors in subsref when the value-name has spaces or special characters?
The SETAPPDATA command is used with a figure handle to assign application-defined data to a variable name. It does not give any warning or error when the name to which data is assigned has spaces or other special characters. However, when it comes to retrieving data, using GETAPPDATA, it results in the error specified below if the name has spaces or other special characters.
Try the following commands in MATLAB:
fig=figure;
setappdata(fig,'line handle',peaks)
getappdata(fig,'line handle')
??? Error using ==> subsref
Invalid field name component.
Error in ==> \\Bat-toaster\R12nightly\toolbox\matlab\graphics\getappdata.m
On line 27 ==> value = subsref(value,struct('type','.','subs',name));
It would be useful if the error is reported in the first step itself when the setappdata is used to assign the data to variable name, 'line handle'.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This error occurs as the SETAPPDATA essentially adds a field with the name, here 'line handle', to a structure stored with the figure handle. A field name of structure can not have spaces or special characters. Thus getappdata fails in retrieving the data. Although if you type 'getappdata(fig)' where fig is handle to the figure in the above code, it will show that 'line handle' contains data but you cannot access it.
In MATLAB 6.5 (R13), you will now get an error when specifing an invalid name using SETAPPDATA.
This problem can be resolved, in earlier versions of MATLAB, by avoiding spaces or other special characters in the name of the variable. For example, 'line handle' should be replaced by 'line_handle'.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!