Error using sprintf Function is not defined for 'matlab.ui​.control.U​IControl' inputs.

5 views (last 30 days)
How to resolve this error in my code
if nargin == 0
% Create a figure with a drop-down menu
figure('Color',[1,1,1]);
h = uicontrol('Style','popup','Position',[15,10,90,21],...
'BackgroundColor',[1,1,1],'Value',2,...
'string',{'sRGB','Y''PbPr','HSV','HSL','L*a*b*','L*u*v*','L*ch'});
set(h,'Callback',sprintf('%s(get(%.20g,''Value''))',mfilename,h));
Cmd = 2;
end
it says Error using sprintf Function is not defined for 'matlab.ui.control.UIControl' inputs.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Nov 2017
me = str2func( ['@', mfilename] );
CellAt = @(C, Idx) C{Idx};
set(h, 'Callback', @(src, event) me( CellAt(get(src, 'String'), get(src, 'Value')) ) );
Your code was written for HG1, and was only ever really reliable on Mac.
There are better ways to code this, such as creating a cell array of function handles and indexing into that cell array.
  1 Comment
Walter Roberson
Walter Roberson on 29 Oct 2020
With regards to my saying that the original code was only reliable on Mac:
Historically, the MS Windows formatting functions (provided by the operating system) produced wrong output when asked to output more than 15 digits. Historically, the Linux formatting functions (provided by the operating system) produced correct output to a few more digits, but then just stopped and put zeros for the rest. But historically, at least for Mac OS X, Mac always had correct output for extended digits.
The correct decimal representation of the numeric handle, h, was important because in HG1 (R2014a and earlier), the exact numeric value, all 53 bits of double precision precision, was used to select the graphics object in the internal tables. If even just the final bit was wrong because not quite enough digits had been output to be able to select the precise final bit, then there would not have been a match on the handle object and the set() call would have failed.

Sign in to comment.

More Answers (1)

mado
mado on 19 Jun 2022
I run my code and i get this error
"Error using sprintf Function is not defined for 'matlab.ui.Figure' inputs"
the code set(gcf, 'Name', sprintf('Fig.%d', gcf), 'NumberTitle', 'off');
  3 Comments
Walter Roberson
Walter Roberson on 19 Jun 2022
Graphics objects exist internally in memory, but users need a way to refer to them.
In current versions of MATLAB, the graphics functions return a "handle object", which is some kind of internal memory pointer to a block of memory that has been marked as being an "object". You cannot tell where the object really is in memory without using special debugging techniques.
In older versions of MATLAB, up to and including r2014a, what got returned to the user was a double precision number. As it was truly a number you could format it and display it. It did not give you any information about where the object really is in memory: it just had the "contract" that as long as the graphics object existed, passing the graphics routines the same double precision number would refer (somehow!) to the same graphic object.
Now, that old system also had the property that when you created figure(), unless you took special steps, that the double precision number associated with the figure would be the lowest unused positive integer. So if you create three figures they would get 1.0, 2.0, 3.0, and if you then closed 2.0 and asked to create another figure the lowest unused integer would be 2.0 and that is what would be allocated. So in the usual course of events you could assume that any figure you created would have a double precision number associated that you could display with %d format
I mentioned that now figures are returned as abstract graphics handles instead. But... you can double() the handle and get back the double precision number, at least for traditional graphics objects. The software still keeps track of those double precision numbers internally, but the user never sees the numbers unless they specifically ask to convert to numeric.

Sign in to comment.

Categories

Find more on Graphics Object Programming 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!