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

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

Related

How to do an animate plot in MATLAB from a sequence of matrices

I have code that uses Wolff's Algorithm to simulate the XY Model in MATLAB and I want to implement a pcolor/color map to demonstrate each spin according to their angles across the system. But I want it to be live and changing as the angles change.
Any idea how to do this?
This is an example of how I want it to look https://i.stack.imgur.com/aSp7s.png
If you save each snapshot of the lattice in a cell array A{t}, you can use the following function to view and save it as a video (if fileName is not empty, the function saves an mp4 video).
Another option is to adapt the function view_lattice to run your simulation (which, honestly, I wouldn't recommend, for performance issues). I will mark where you should edit for doing a "live" simulation
This is at least MATLAB R2019b (although it may be compatible with earlier versions, but no guarantee).
File view_lattice.m
function view_lattice(A,fileName)
% for a 'live' simulation, you will have to remove A from the input
% parameters and add the ones you need for the XY Wolff algorithm,
% which will be used to calculate each configuration A in the time loop below
% you will also need to remove the assert statements for 'live' simulation
%
% otherwise, you save snapshots from your simulation
% and use this function as is
%
% A -> A{k}[m,n] snapshot k containing the angles of spins in lattice site at row m and col n
% fileName -> if contains string, then records a video with the snapshots and name it with this string
assert(iscell(A) && all(cellfun(#(a)isnumeric(a) && ismatrix(a),A)),'A must be cell of numeric matrices');
assert(ischar(fileName),'fileName must be either an empty char or contain a file name');
recordVideo = ~isempty(fileName);
if recordVideo
vw = setup_video(fileName);
else
vw = [];
end
% setting some default axis properties to speed-up plotting
set(0,'DefaultAxesPlotBoxAspectRatio',[1 1 1],'DefaultAxesDataAspectRatioMode','manual','DefaultAxesDataAspectRatio',[1,1,1],'DefaultAxesNextPlot','replace');
fh = figure;
ax=axes;
for t = 1:numel(A) % for 'live' simulation, this loop should be the time loop
% here you calculate the new configuration A
% and call the function below with A instead of A{t}
vw = record_frame(vw,fh,ax,A{t},t,recordVideo);
end
% any video to close?
if recordVideo
vw.close();
end
end
function vw = record_frame(vw,fh,ax,A,t,recordVideo)
imagesc(ax,A);
title(ax,sprintf('snapshot %g',t)); % if you want, y
axis(ax,'square');
daspect(ax,[1,1,1]);
pause(0.01);
if recordVideo
vframe = getframe(fh);
vw.writeVideo(vframe);
end
end
function vw = setup_video(fileName)
vid_id = num2str(rand,'%.16g');
vid_id = vid_id(3:6);
vid_id = [fileName,'_',vid_id];
% Initialize video
vw = VideoWriter([vid_id,'.mp4'], 'MPEG-4'); %open video file
vw.Quality = 100;
vw.FrameRate = 16;
vw.open();
end
Test script: test.m
clearvars
close all
A = cell(1,30);
for t = 1:numel(A)
% creating a sequence of random snapshots only for illustration
A{t} = rand(20,20);
end
% viewing the animation and saving it as a video with name test
view_lattice(A,'test');
Output

Error using legappend()

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

MATLAB Data Cursor Mode

I have a simple class that plots basic x and y data. Within the class I have a method that enables the data cursor mode, customizes the text, and collects and saves the points. I'd like to change the behavior of the method so that I can collect only two points at a time. Right now it stores every point even when I turn off the data cursor mode and turn it back on to use it. Here is my code for my class:
classdef CursorPoint
properties
Xdata
Ydata
end
methods
function me = CursorPoint(varargin)
x_data = 0:.01:2*pi;
y_data = cos(x_data);
f= figure;
plot(x_data,y_data);
me.DCM(f);
end
function DCM(me,fig)
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',#myupdatefcn)
set(dcm_obj,'enable','on')
myPoints=[];
function txt = myupdatefcn(empt,event_obj)
% Customizes text of data tips
pos = get(event_obj,'Position');
myPoints(end + 1,:) = pos;
txt = {['Time: ',num2str(pos(1))],...
['Amplitude: ',num2str(pos(2))]};
end
end
end
end
Could you change the myPoints variable to two variables called myPointCurrent and myPointPrevious. When ever the myupdatefcn method is called you would move the contents of myPointCurrent into myPointPrevious and then store the current position in myPointCurrent.
The new function (with some error checking) would look something like:
function txt = myupdatefcn(empt,event_obj)
% Customizes text of data tips
myPointPrevious=myPointCurrent;
pos = get(event_obj,'Position');
myPointCurrent=pos;
txt = {['Time: ',num2str(pos(1))],...
['Amplitude: ',num2str(pos(2))]};
end

Arguments in #functions [duplicate]

This question already has answers here:
Matlab Callback Function Only Sees one Parameter Passed to it
(3 answers)
Closed 8 years ago.
I am making a self function to change te text displayed by the cursor in my GUI figure. This is what I have done by the time:
dcm=datacursormode(hAxes.figure);
datacursormode on
set(dcm,'update',#myfunction)
function output_txt = runnumber(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');
%getCursorInfo(dcm)
%inputDrDataCell
% Get the handle to the data cursor.
menu = findall(get(gcf,'Children'),'Type','uicontextmenu');
menuCallback = get(menu,'Callback');
dataCursor = menuCallback{2};
% Get the coordinates if a datatip exists.
info = getCursorInfo(dataCursor);
if ~isempty(info)
number = info.DataIndex
end
output_txt = {['X: ',num2str(pos(1),4)],...
['Y: ',num2str(pos(2),4)],...
['Run number:',num2str(number)]};
% 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
end
However, I would like to pass more input arguments to #myfunction in order to display the name of the axis, the raw data file etc.
Any help?
Additional arguments are supplied to callbacks by using a function handle and the additional arguments in a cell:
set(dcm,'update',{#myfunction,arg3,arg4});
These correspond to the third and fourth inputs to your function:
function output_txt = runnumber(obj,event_obj,arg3,arg4)
Another way to do this, nothing wrong with Hugh Nolan's answer, is to use an anonymous function handle like so:
set(dcm, 'update', #(obj,event) runnumber(obj,event,arg3,arg4));
HTH!

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