Error using legappend() - matlab

I am writing a function that iterates through a loop and adds entries to a plot. When I try to use legappend(), though, I get the error below. I am passing it a string variable.
Error using legend>process_inputs (line 526)
Invalid argument. Type 'help legend' for more information.
Error in legend>make_legend (line 303)
[orient,location,position,children,listen,strings,propargs] =
process_inputs(ha,argin); %#ok
Error in legend (line 257)
[~,msg] = make_legend(ha,args(arg:end),version);
Error in legappend (line 74)
[legend_h,object_h,plot_h,text_strings] = legend(h,allDatah,str);
Here is a minimal example, taken from the MATLAB site
% Some data and old models:
x = (1:10)';
y = [x-5+x.^1.05 x-2 x-3 x-4 x-5];
% Plot the data and old models:
figure
plot(x,y(:,1),'mo','markersize',10);
hold on;
plot(x,y(:,2),'r');
plot(x,y(:,3),'b');
plot(x,y(:,4),'k');
plot(x,y(:,5),'kp');
box off
axis([1 10 -5 20])
legend('data','model 1','model 2','model 3','model 4','location','northwest')
legend boxoff
myNewModel = x - 5.5 + x.^1.1;
plot(x,myNewModel,'m','linewidth',2);
legappend('my new amazing model!')

As mentioned in my comment, MATLAB's graphics engine was significantly overhauled in R2014b. While it brought a plethora of very welcome changes, like any major code overhaul it broke functionality in the existing code base. Relevant here is implementing legends as their own object class rather than as a cobbled together axes object. Given its July 2014 release date, legappend was likely created using R2014a and the logic in the code assumes that the legend is an axes object. This unfortunately breaks in the new graphics system.
Fortunately, the fix isn't as complex as I was anticipating. If you take a look at the properties of the new legend object, there's no documented property for the linked data. Attempting to set the 'String' property manually also has no effect. However, if you look at the final syntax in the function description ([l,icons,plots,txt] = legend(___)), it seems clear that legend has a way to access the appropriate internal properties. And indeed, if you poke around in the legend source, you'll find the 'PlotChildren' property, which is an array of object handles.
Putting it all together we get something like the following:
function legappend_HG2(newStrings)
% Quick & dirty fork of legappend specific to MATLAB versions >= R2014b
% Only supports appending strings to the existing legend handle
% Assumes only one legend is in the current figure
% Add multiple strings by passing it a 1D cell array of strings
% Find our legend object
h = findobj(gcf, 'Type', 'legend');
if ~isempty(h)
% Get existing array of legend strings and append our new strings to it
oldstr = h.String;
if ischar(newStrings)
% Input string is a character array, assume it's a single string and
% dump into a cell
newStrings = {newStrings};
end
newstr = [oldstr newStrings];
% Get line object handles
ploth = flipud(get(gca, 'Children'));
% Update legend with line object handles & new string array
h.PlotChildren = ploth;
h.String = newstr;
end
end
Swapping legappend for legappend_HG2 in the above MWE we get the desired result:

Was getting wrong result in R2016b for exkaza's solution. Hence changed the code in a line a little bit.
function legappend_HG2(newStrings)
% Quick & dirty fork of legappend specific to MATLAB versions >= R2016b
% Only supports appending strings to the existing legend handle
% Assumes only one legend is in the current figure
% Add multiple strings by passing it a 1D cell array of strings
% Find our legend object
h = findobj(gcf, 'Type', 'legend');
if ~isempty(h)
% Get existing array of legend strings and append our new strings to it
oldstr = h.String;
%disp(oldstr);
if ischar(newStrings)
% Input string is a character array, assume it's a single string and
% dump into a cell
newStrings = {newStrings};
end
newstr = [oldstr newStrings];
%disp(newstr);
% Get line object handles
ploth = flipud(get(gca, 'Children'));
% Update legend with line object handles & new string array
%h.PlotChildren = ploth;
h.PlotChildren = [h.PlotChildren; ploth];
h.String = newstr;
end
end

Related

MATLAB: Cannot parse multiple floating-point values

I have a GUI with a UITable (built in GUIDE). I read in two numerical values and through a series of steps convert the floating-point value to a string. I want the user to have the ability to click on a specific cell in the UITable containing the two values (now as strings), and read those values back as floating-point values. For whatever reason, I can only ever get my code to read in the first floating-point value. My code (in order) is below.
Step 1: Access data and convert to string and place string in corresponding column.
function fillAnnotRangeInfo(obj, selectedAxes)
selectedAxesTag = selectedAxes.Tag;
rawAxesTag = obj.rawDataDisplayAxes.Tag;
psdAxesTag = obj.psdDataDisplayAxes.Tag;
% Depending on which axes the user clicks on, the units will either be Hz or s
if strcmp(selectedAxesTag, rawAxesTag)
dataRange = strcat(num2str(obj.t1), {'s'}, {' - '}, num2str(obj.t2), {'s'});
elseif strcmp(selectedAxesTag, psdAxesTag)
dataRange = strcat(num2str(obj.t1), {'Hz'}, {' - '}, num2str(obj.t2), {'Hz'});
end
obj.nextRow.AnnotRange = dataRange;
end
Step 2: Determine if user clicked in correct cell and attempt to read two floating-point values out.
% --- Executes when selected cell(s) is changed in existingAnnotationsTable.
function existingAnnotationsTable_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to existingAnnotationsTable (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
% AE = handles.UserData;
Indices = eventdata.Indices;
% Determine if column is column of interest
if Indices(2) == 4
rangeData = handles.existingAnnotationsTable.Data;
rangeData = rangeData{Indices(1), Indices(2)};
annoRange = sscanf(rangeData, '%f')
else
end
return
Ultimately, the result I get is if I have a string exactly as follows: "7.4250Hz - 32.502Hz" (or replace Hz with "s"), my program will only produce "7.4250". Nothing more nothing less. I've tried textscan, sscanf, and strread. Each one I explicitly set my filter to floating-point (%f). With strread I tried setting it to cycle through more than once (strread('string', %f, 2)). I have no idea what else to do or try or change.
REGARDING THE ANSWERS BELOW FOR FUTURE READERS: Technically any one of the answers is "correct" (I tried them). They all are applicable to a slightly different situation. Ben's answer is good for getting a result in a single step if you have a fixed format. My own answer works to break down a string across multiple steps giving access to the data each step (useful for performing multiple operations) whilst still being able to deal with varying formats. Andras' answer is useful for getting it done in one step providing a result instantly whilst still being able to deal with varying formats.
Have a look at the sscanf documentation here, if you want a set of floats from a string you need to specify the format. Here is an example from this page similar to yours:
tempString = '78°F 72°F 64°F 66°F 49°F';
degrees = char(176);
tempNumeric = sscanf(tempString, ['%d' degrees 'F'])'
tempNumeric =
78 72 64 66 49
In your specific case you could try:
val = sscanf(rangedata,'%fHz - %fHz')
Ben's answer is the correct answer in the case of a fixed format. However, in my case, my format changes. Therefore my proposed solution (tested and verified) is to use strtokto "break-down" the string into two separate strings, making it much more manageable and easier to parse. Code below. Thoughts?
% --- Executes when selected cell(s) is changed in existingAnnotationsTable.
function existingAnnotationsTable_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to existingAnnotationsTable (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
% AE = handles.UserData;
Indices = eventdata.Indices;
% Determine if column is column of interest
if Indices(2) == 4
rangeData = handles.existingAnnotationsTable.Data;
rangeData = rangeData{Indices(1), Indices(2)};
[dataStart, dataRemain] = strtok(rangeData);
% In this case, since there will be just a '-' as the token...
% ...we don't care about the token and only save the remainder.
[~, dataEnd] = strtok(dataRemain);
dataStart = sscanf(dataStart, '%f')
dataEnd = sscanf(dataEnd, '%f')
else
end
Motivated by my own comment about using regexp before sscanf, here's a solution which only uses the former:
str=regexp(data,'(?<dataStart>[\d\.]+)[^\d\.]+(?<dataEnd>[\d\.]+)','names');
after which str will have the fields dataStart and dataEnd as strings (so you'll need num2str afterwards). Note that this only works for simple floating point numbers (but of course the regexp could be further complicated ad nauseam if necessary). The upside is that it can be adapted to more tricky input strings, for instance the above will treat any number and kind of non-numeric (and non-dot) text between the first two numbers. The downside is regexp.

How to properly display TeX strings in axes' datatips? (MATLAB hg2)

I have recently tried to run an old piece of code (written on hg1) on a new version of MATLAB (2015a) that has hg2.
I used to be able to do the following (according to the "gnovice-Amro" method):
function output_txt = customDatatip(~,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
hFig = ancestor(event_obj.Target,'figure'); %// I don't trust gcf ;)
pos = get(event_obj,'Position');
output_txt = {['\lambda: ',num2str(pos(1)*1000,4) 'nm'],...
['T(\lambda): ',num2str(pos(2),4) '%']};
set(findall(hFig, 'Type','text', 'Tag','DataTipMarker'),...
'Interpreter','tex'); %// Change the interpreter
And would get nicely formatted datatip labels with Greek characters.
However, in the new hg2 system, findall returns a 0x0 empty GraphicsPlaceholder array, which renders setting the Interpreter useless.
My question is: How can I set the plot datatip interpreter to (La)TeX in hg2?
After some digging using uiinspect, I found that the "TextBox" is now stored as a matlab.graphics.shape.internal.GraphicsTip type of object within obj's TipHandle property which, in turn, has an Interpreter property! Both properties are public and can be set easily using dot notation. I've ended up using the following code:
function output_txt = customDatatip(obj,event_obj)
% Display the position of the data cursor // <- Autogenerated comment
% obj Currently not used (empty) // <- Autogenerated comment, NO LONGER TRUE!
% event_obj Handle to event object // <- Autogenerated comment
% output_txt Data cursor text string (string or cell array of strings). // <- A.g.c.
hFig = ancestor(event_obj.Target,'figure');
pos = get(event_obj,'Position');
output_txt = {['\lambda: ',num2str(pos(1)*1000,4) 'nm'],...
['T(\lambda): ',num2str(pos(2),4) '%']};
if ishg2(hFig)
obj.TipHandle.Interpreter = 'tex';
else %// The old version, to maintain backward compatibility:
set(findall(hFig, 'Type','text', 'Tag','DataTipMarker'),...
'Interpreter','tex'); % Change the interpreter
end
function tf = ishg2(fig)
try
tf = ~graphicsversion(fig, 'handlegraphics');
catch
tf = false;
end
Notes:
The first input to the function (obj) is no longer ignored, since it has some use now.
The ishg2 function is taken from this MATLAB Answer.
Edit1:
Just noticed that there's another way to check MATLAB's Graphic version (i.e. hg1/hg2) using the following code I found in the wavelet toolbox:
function bool = isGraphicsVersion2
%//isGraphicsVersion2 True for Graphic version 2.
%// M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 21-Jun-2013.
%// Last Revision: 04-Jul-2013.
%// Copyright 1995-2013 The MathWorks, Inc.
%// $Revision: 1.1.6.1 $ $Date: 2013/08/23 23:45:07 $
try
bool = ~matlab.graphics.internal.isGraphicsVersion1;
catch
bool = ~isprop(0,'HideUndocumented');
end

Accessing graphic object properties using dot notation on Matlab versions anterior to R2014b

Trying to change the colors of an Axis in a matlab plot here.
Referencing matlab documentation: Matlab docs on setting axis properties
Code snippet:
subplot( 'Position', [ left bottom (1/(cols*2)) (1/rows) ] );
ax = gca;
ax.Color = 'y';
That's all but a copy and paste from the example in the docs (shown here):
But matlab throws up a warning and doesn't change the axis colors for me:
Warning: Struct field assignment overwrites a value with class
"double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure
Variables As Structures Displays Warning, for details.
I tried assigning a double, like say 42.0, but it didn't like that any better.
Your warning message seems to indicate you are using a version anterior to Matlab R2014b.
If it is that, you do not have access to the dot notation directly because when you do ax=gca; you get a return value ax which is of class double. The value is the ID of the handle to the object (the current axis in this case) but not the handle itself.
When you try ax.Color = 'y';, Matlab thinks you want to overwrite your ax [double] with a new variable ax which would be a structure, with the field color, and throw a warning.
You can still access the dot notation for the graphic obects and properties but you have to first retrieve the real handle of the object, by using the function handle. For example:
ax = handle( gca) ; %// the value "ax" returned is an `object`, not a `double`
or even on an existing reference to a graphic object handle:
ax = gca ; %// retrieve the `double` reference to the handle
...
ax = handle(ax) ; %// the value "ax" returned is an `object`, not a `double`
after that you should be able to use the dot notation for all the public properties of the graphic object. ax.Color = 'y'; should now be valid

plot automatically titled with plot object in matlab

I would like to know if some of you know of a way to automatically title plots with the name of the object plotted
so that for intance when I plot supermatrix(5:10,:,2:3)
the title (or the legend ..) on the plot says "supermatrix(5:10,:,2:3)"
thanks
Is this for debugging purposes? If not then I suggest you tell us your overall motivation because someone might be able to suggest a more robust method, but this might get you started:
vname = #(x)inputname(1); %//from here: https://www.mathworks.com/matlabcentral/newsreader/view_thread/251347
plot(supermatrix(5:10,:,2:3))
title(vname(supermatrix))
Although to be honest I cannot imagine why this would ever be useful
I think this does what you want and remains pretty flexible:
function h = plotwithtitle( plotstring, varargin )
argstoplot = evalin('caller', ['{', plotstring, '}']);
h = plot( argstoplot{:}, varargin{:} );
title(plotstring);
end
The following examples all work for me:
supermatrix=rand(10,10);
x=1:10;
y=rand(1,10);
plotwithtitle('supermatrix');
plotwithtitle('supermatrix(5:10,:)');
plotwithtitle('x, y');
plotwithtitle('x, y', '--r');
plotwithtitle('1:10', 'r');
plotwithtitle('rand(1,10)');
I've modified the function dfig originally created by F.Moisy for creating docked figures in order to have the command used for plotting show up in the figure name.
The idea is to read the last command in the command history and use that to generate the figure title.
function hh = dfig(varargin)
%DFIG Create docked figure window
% DFIG, by itself, creates a new docked figure window, and returns its
% handle.
%
% DFIG(H) makes H the current figure and docks it. If Figure H does not
% exist, and H is an integer, a new figure is created with handle H.
%
% DFIG(H, name, value,...) reads additional name-value pairs. See
% doc(figure) for available otions.
%
% DFIG will parse the command line input and use the text following dfig
% as figure name. E.g. calling dfig,plot(x(1:3),y(2:2:end)) results in
% the name "plot(x(1:3),y(2:2:end))"
% F. Moisy, moisy_at_fast.u-psud.fr
% Revision: 1.00, Date: 2007/09/11
% Modified (a lot) by Jonas
if nargin==0
h=figure; % create a new figure
else
% call figure with varargin
figure(varargin{:})
h = gcf;
end
if ~any(strcmp('name',varargin(1:2:end)))
% if no name has been supplied: try to use function call
javaHistory=com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
if ~isempty(javaHistory)
lastCommand = javaHistory(end).toCharArray';
funCall = regexp(lastCommand,'dfig\s*[,;]\s*(.*)$','tokens','once');
else
funCall = [];
end
if ~isempty(funCall)
if isnumeric(h)
set(h,'Name',[num2str(h),': ',funCall{1}],'NumberTitle','off')
else % HG2
h.Name = sprintf('%i: %s',h.Number,funCall{1});
h.NumberTitle = 'off';
end
end
end
set(h,'WindowStyle','docked'); % dock the figure
if nargout~=0 % returns the handle if requested
hh=h;
end

How can I display numbers with higher precision in a MATLAB data cursor?

I have a problem with precision loss. I imported a set of values from a CSV file into MATLAB 7 using the following code:
function importfile(fileToRead1)
%#IMPORTFILE(FILETOREAD1)
%# Imports data from the specified file
%# FILETOREAD1: file to read
DELIMITER = ',';
HEADERLINES = 0;
%# Import the file
rawData1 = importdata(fileToRead1, DELIMITER, HEADERLINES);
%# For some simple files (such as a CSV or JPEG files), IMPORTDATA might
%# return a simple array. If so, generate a structure so that the output
%# matches that from the Import Wizard.
[~,name] = fileparts(fileToRead1);
newData1.(genvarname(name)) = rawData1;
%# Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
This very basic script just takes the specified file:
> 14,-0.15893555
> 15,-0.24221802
> 16,0.18478394
And converts the second column to:
14 -0,158935550000000
15 -0,242218020000000
16 0,184783940000000
However, if I select a point with the Data Cursor it only displays 3 or 4 digits of precision:
Is there a way to program a higher precision to get more exact data points?
Your data isn't losing precision, the Data Cursor display just isn't showing the full precision so that the text boxes are a more reasonable size. However, if you want to increase the precision of the display in the text datatip, you can customize it.
If you right click on a Data Cursor text box, you should see a menu like this:
If you then select the Edit Text Update Function... option, it will open a default m-file containing the following:
function output_txt = myfunction(obj, event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj, 'Position');
output_txt = {['X: ', num2str(pos(1), 4)], ...
['Y: ', num2str(pos(2), 4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ', num2str(pos(3), 4)];
end
Notice that the text for the X and Y coordinate data is formatted using num2str, with the second argument being a 4. This converts the coordinate value to a string representation with 4 digits of precision. If you want more digits displayed, simply increase this number, then save the newly-created m-file on your path.
Now your datatip text should display more precision for your numbers. If you want to accomplish all of the above programmatically, you would first create your text update function, save it to a file (like 'updateFcn.m'), then turn on Data Cursors using the function datacursormode and set them to use your user-defined text update function. Here's an example:
plot(1:10, rand(1, 10)); % Plot some sample data
dcmObj = datacursormode; % Turn on data cursors and return the
% data cursor mode object
set(dcmObj, 'UpdateFcn', #updateFcn); % Set the data cursor mode object update
% function so it uses updateFcn.m
If you want to make a permanent change - Warning: This is a slight hack to MATLAB - open:
C:\Program Files\Matlab\R2007b\toolbox\matlab\graphics\#graphics\#datacursor\default_getDatatipText.m
or a similar file depending on your version and change DEFAULT_DIGITS.
Don't quote me on this, but:
1) You haven't lost precision, MATLAB stores the full value, it is only the display that has been pared down.
2) In my version of MATLAB (R2009a) I can modify the way long numbers are shown in the command menu by going to
File>Preferences>Variable Editor
where a dropdown menu lets me pick between short, long, short e, long e, short g, long g, short eng, long eng, bank, + and rat.
I have no idea whether that affects what the Data Cursor shows, though.
You can add the following in your script:
dcm_obj = datacursormode(fig);
set(dcm_obj,'Updatefcn',#myfunction_datacursor);
You need to create and save myfunction_datacursor file with the following in your path (get path by calling path in MATLAB prompt)
function output_txt = myfunction_datacursor(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),8)],...
['Y: ',num2str(pos(2),4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),8)];
end