plot automatically titled with plot object in matlab - 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

Related

How to refer the figure name in matlab

I would like to define a figure and then for that specific figure do some things. Foe example: I would like to vreat a digure say:
h1 = figure('units','normalized','outerposition',[0 0 1 1]);
and then I want for h1 to do for example:
subplot(1,3,1)
plot(N_vec,1./Err(N_vec),sprintf('*%c',colconds(loop)),'LineWidth',5)
hold on
plot(N_vec,1./ErrPV(N_vec),sprintf('*%c',colconds(loop+2)),'LineWidth',5)
hold on
xlabel('Population size','fontsize',20)
ylabel('Error^-2 ','fontsize',20)
legend('OLE','PV','OLE shuffled','PV shuffled','Location','northwest')
The thing is that from loop reasons, h1 is defined far from the above lines. and is not the current figure handel. So I want the above lines to refer specifically for h1. somethong like:
subplot('h1',1,3,1)
plot('h1',N_vec,1./Err(N_vec),sprintf('*%c',colconds(loop)),'LineWidth',5)
hold on
plot('h1',N_vec,1./ErrPV(N_vec),sprintf('*%c',colconds(loop+2)),'LineWidth',5)
hold on
xlabel('h1','Population size','fontsize',20)
ylabel('h1','Error^-2 ','fontsize',20)
legend('h1','OLE','PV','OLE shuffled','PV shuffled','Location','northwest')
But matlab gives error whenever i try to add the name h1 in the specific commands for the figure...
how do I refer the figure handel when I actully want to use it?
Thanks!!
You could assign a 'Name' or 'Tag' to the h1 figure and then use that as a reference to get it before plotting.
% Create a figure with tag 'MyFig'
figure('Tag', 'MyFig');
% ... later in the code ...
% Get the figure with the Tag "MyFig"
h1 = findobj('Type', 'Figure' ,'Tag', 'MyFig')
Tag is for this purpose better than Name as the later is shown after the figure number. The Type-Figure argument could be skipped, but narrows down the objects to search.
Now you can use h1 as a regular handle.
subplot(h1,1,3,1)
plot(h1, ...)
You could also set h1 figure to the current figure
set(groot, 'CurrentFigure', h1);
% ... or for older versions
set(0, 'CurrentFigure', h1)
Then you could omit the handle in the plot commands
subplot(1,3,1)
plot(...)

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

Saving GUI data from matlab in a 3D matrix

I'd like to save matrices which are input via GUI pushbuttons in a 3D matrix. for example: I click button 1 a 2-2 matrix is put in the first slice of the 3D matrix. I than click button 3 and a different 2-2 matrix is put in the second slice. SO on and so on. I hope this is clear enough. The problem I have is that I'm not an experienced programmer. I currently initialize a zeros matrix as follows in the opening func:
storageMatrix = ones(2,2,100);
setappdata(0, 'storageMatrix', storageMatrix);
I have put a function updateStorageMatrix in the main script like this, mind you this isn't finished yet.
function updateStorageMatrix
storageMatrix = getappdata(0, 'storageMatrix');
and than when I look at my code of pushbutton 1 for example it looks like this:
% --- Executes on button press in Add_Straightduct.
function Add_Straightduct_Callback(hObject, eventdata, handles)
%prompt command for k number and length
prompt = {'k0:','Length:'};
dlg_title = 'Straight Duct specs';
num_lines = 1;
SDelements = {'0','0'};
Straightduct = inputdlg(prompt,dlg_title,num_lines,SDelements)
%insert values in function
sizeStorageMatrix = size(getappdata(0,'storageMatrix')); %get size of the storage matrix
dimT = sizeStorageMatrix(1,3); %take the number of matrices
if dimT==1
straightDuct = straight_duct(str2num(SDelements{1}), str2num(SDelements{2}), Mach_Frange(1,1))
setappdata(handles.updateStorageMatrix,'storageMatrix', storageMatrix(1:2, 1:2, 1))=straight_duct(str2num(SDelements{1}), str2num(answer{2}), Mach_Frange(1,1))
dimT+1
else
setappdata(0,'storageMatrix', storageMatrix(1:2, 1:2, dimT+1))=straight_duct(str2num(SDelements{1}), str2num(answer{2}), Mach_Frange(1,1))
dimT+1
end
the straight_duct() is a function I used in the script when calculating the mufflers, I have several of those functions, I am not sure if that's the way to work when using GUI. I just hope you get the idea of what I'm trying to achieve and help me on my way. So I can actually make a UI for my script :)
This assumes you've initialized the storageMatrix elsewhere in the GUI like this...
handles.storageMatrix = zeros(2,2,100);
guidata(hObject,handles); % Must call this to save handles so other callbacks can access the new element "storageMatrix"
Then in your first button's callback...
% --- Executes on button press in Add_Straightduct.
function Add_Straightduct_Callback(hObject, eventdata, handles)
.
. % Whatever initializations you need to do
.
localStorageMatrix = handles.storageMatrix; %load the storageMatrix being used by other callbacks/functions
.
. % Whatever you need to do to generate the new 2x2 matrix "slice"
.
localStorageMatrix(:,:,end+1) = newSlice; % appends the new slice to the end of the, indexing using colons assumes newSlice is of the same first and second dimension as other slices in localStorageMatrix
handles.storageMatrix = localStorageMatrix; % overwrite the storageMatrix in handles with the contents of the new localStorageMatrix
guidata(hObject,handles); % save the handles struct again now that you've changed it
Alternatively, you could have just used the handles.storageMatrix throughout, without including a localStorageMatrix but I've included it for emphasis.

Matlab Edit Text Boxes - Show Hint?

When working with Matlab GUIs, is there a way to place a "hint" in an Edit Text box? That is, text that will disappear once the user starts typing? I've used similar functionalities in Android, but I'm not as familiar with other GUIs, so I'm not sure how widespread this functionality is.
This is possible in Matlab, but you have to define a custom MouseClickCallback, which is only accessible using findjobj by Yair Altman (download it from the Matlab File Exchange using the link and save it somewhere on your Matlab path).
Since I like the idea, I have written a function that conveniently takes care of all this. It creates a grayed-out, italic help text that disappears once you click the edit box.
function setInitialHelp(hEditbox,helpText)
%SETINITIALHELP adds a help text to edit boxes that disappears when the box is clicked
%
% SYNOPSIS: setInitialHelp(hEditbox,helpText)
%
% INPUT hEditbox: handle to edit box. The parent figure cannot be docked, the edit box cannot be part of a panel.
% helpText: string that should initially appear as help. Optional. If empty, current string is considered the help.
%
% SEE ALSO uicontrol, findjobj
%
% EXAMPLE
% fh = figure;
% % define uicontrol. Set foregroundColor, fontAngle, before
% % calling setInitialHelp
% hEditbox = uicontrol('style','edit','parent',fh,...
% 'units','normalized','position',[0.3 0.45 0.4 0.15],...
% 'foregroundColor','r');
% setInitialHelp(hEditbox,'click here to edit')
%
% check input
if nargin < 1 || ~ishandle(hEditbox) || ~strcmp(get(hEditbox,'style'),'edit')
error('please supply a valid edit box handle to setInitialHelp')
end
if nargin < 2 || isempty(helpText)
helpText = get(hEditbox,'string');
end
% try to get java handle
jEditbox = findjobj(hEditbox,'nomenu');
if isempty(jEditbox)
error('unable to find java handle. Figure may be docked or edit box may part of panel')
end
% get current settings for everything we'll change
color = get(hEditbox,'foregroundColor');
fontAngle = get(hEditbox,'fontangle');
% define new settings (can be made optional input in the future)
newColor = [0.5 0.5 0.5];
newAngle = 'italic';
% set the help text in the new style
set(hEditbox,'string',helpText,'foregroundColor',newColor,'fontAngle',newAngle)
% add the mouse-click callback
set(jEditbox,'MouseClickedCallback',#(u,v)clearBox());
% define the callback "clearBox" as nested function for convenience
function clearBox
%CLEARBOX clears the current edit box if it contains help text
currentText = get(hEditbox,'string');
currentColor = get(hEditbox,'foregroundColor');
if strcmp(currentText,helpText) && all(currentColor == newColor)
% delete text, reset color/angle
set(hEditbox,'string','','foregroundColor',color,'fontAngle',fontAngle)
else
% this is not help text anymore - don't do anything
end
end % nested function
end % main fcn

Updating MATLAB waitbar fails after first update

I am having a problem updating a waitbar in a MATLAB GUI.
I created a simple example that works as expected.
steps = 5;
hWaitBar = waitbar(0, 'Testing...');
for i = 1:steps
waitbar(i/steps, hWaitBar);
pause(1);
end
close(hWaitBar);
However when I use this construction in the GUI...
numSteps = %calculated
hWaitBar = waitbar(0, 'Processing...');
if %conditional
for i = 1:numSteps
waitbar(i/numSteps, hWaitBar)
% additional processing
end
else %conditional
% additional processing
end
close(hWaitBar);
...the waitbar only displays correctly for the first for loop iteration.
The second interation fails with the execption:
Error using waitbar(109)
Improper arguments for waitbar.
I have verified that the waitbar progress value does not exceed 1.
I have verified that the waitbar is not being closed until outside the if/else loop.
I found the solution -- I was misuing handle graphics.
In my original code:
numSteps = %calculated
hWaitBar = waitbar(0, 'Processing...');
if %conditional
for i = 1:numSteps
waitbar(i/numSteps, hWaitBar)
% additional processing
% *** call to imagesc caused the error
end
else %conditional
% additional processing
end
close(hWaitBar);
Using the debugger, I saw that the waitbar became the current figure and imagesc tried to plot to it instead of the axis on the main form. Setting the appropriate figure as current immediately before the referencing calls yielded proper behavior.
Corrected code:
numSteps = %calculated
hForm = gcf; % save the handle of the main form
hWaitBar = waitbar(0, 'Processing...');
if %conditional
for i = 1:numSteps
% set the waitbar to be the current figure before it is updated
% note: this syntax will ensure window order will be preserved
% with waitbar on top
set(0, 'CurrentFigure', hWaitBar);
waitbar(i/numSteps, hWaitBar);
% additional processing
set(0, 'CurrentFigure', hForm);
imagesc(...);
% more processing
end
else %conditional
% additional processing
end
close(hWaitBar);