Adding text to a figure - MATLAB - matlab

I wrote a code that shows a figure devided to 2 parts;
the first one showing the main image, and the second one is a slider showing the rest of the images.
Now I need to add text to the main part (Like "Help" or "guide" text).
How can I do it?
This is my main sub-code:
%# design GUI
numSubs = 10; % Num of sub-images.
mx = numImgs-numSubs+1;
hFig = figure('Menubar','none');
% The Main Image:
hAx = axes('Position',[0 0.3 1 0.8], 'Parent',hFig);
hMainImg = imshow(img, 'Parent',hAx);
% the slider
hPanel = uipanel('Position',[0 0.04 1 0.26], 'Parent',hFig);
uicontrol('Style','slider', 'Parent',hFig, ...
'Callback',#slider_callback, ...
'Units','normalized', 'Position',[0 0 1 0.04], ...
'Value',1, 'Min',1, 'Max',mx, 'SliderStep',[1 10]./mx);
subImg = zeros(numSubs,1);
for i=1:numSubs
%# create axis, show frame, hookup click callback
hAx = axes('Parent',hPanel, ...
'Position',[(i-1)/numSubs 0 1/numSubs 1]);
% Load img number i
name=frames(i).name;
img=imread(name,'jpg');
subImg(i) = imshow(img, 'Parent',hAx);
value = i;
set(subImg(i), 'ButtonDownFcn',{#click_callback value})
axis(hAx, 'normal')
hold off;
end
Any suggestions?
Thanks in advance.

Use this construction:
hT = uicontrol('style', 'text', 'string', 'HELLO WORLD', 'position', [...])
It will create static text in the figure at position position. You can use all the regular options for uicontrols like 'parent' or 'units'.
However, since your image is in an axis, the better/easier way to do it is using
hT = text(X, Y, 'HELLO WORLD')
with X and Y the desired coordinates of the text in the axes.
You can set additional options via set:
set(hT, 'color', 'r', 'backgroundcolor', 'k', 'fontsize', 10, ...)
You can get a list of all options by issuing set(hT) on a mock text object.

Related

Brushing multiple plots in one go

I have a series of lines which I would like to mark interactively (using brush). For this purpose, I tried adapting a similar code found here. However that code is not suitable for my use case because it is written with a single line in mind, whereas I have multiple lines (plotted on a single axes).
Hence, I extended the code to achieve my goal as follows:
function testbrushcode()
% Create data
t = 0:0.2:25;
x = sin(t);
y = tan(2.*t);
z = sin(2*t);
% Create figure with points
for i = 1:3
myfig = figure();
m{1} = plot(t, x);
hold on
m{2} = plot(t, y);
m{3} = plot(t, z);
hold off
brush on;
index_vec1{i} = brushed_Data_ids(myfig, m{i});
end
plot(t(index_vec1{1}), x(index_vec1{1}))
hold on
plot(t(index_vec1{2}), x(index_vec1{2}))
plot(t(index_vec1{3}), x(index_vec1{3}))
hold off
end
function [index_vec1] = brushed_Data_ids(myfig, m)
uicontrol('Parent', myfig, ...
'Style', 'pushbutton',...
'String', 'Get selected points index',...
'Position', [5, 5, 200, 30],...
'Units', 'pixels',...
'Callback', {#mycallback, m} ...
);
% ---> Now the user should select the points and click the button 'Get
% selected points index'
waitfor(myfig)
% Display index of selected points once the figure is closed
% disp(selectedPoints);
index_vec1 = [];
for i = 1:length(selectedPoints)
if selectedPoints(i) == 1
index_vec1 = [index_vec1 i];
end
end
end
function mycallback(~, ~, mylineseries)
% Ignore the first 2 function inputs: handle of invoking object & event
% data
assignin('caller', 'selectedPoints', get(mylineseries,'BrushData'))
end
There is a problem in the code stemming from using a for loop - because of it, I have to brush the same data thrice. I want to brush the data once and get the brushed indices (from the unbrushed data) for all three lines.
Try this somewhat simplified code:
function q60017140()
% Create data
t = 0:0.2:25;
x = sin(t);
y = tan(2*t);
z = sin(2*t);
% Create figure with points
hFig = figure('Position', [295,303,1014,626]);
hAx = subplot(1,2,1,'Parent', hFig);
hLines = plot(hAx, t,x, t,y, t,z);
hAx(2) = subplot(1,2,2);
uicontrol('Parent', hFig, ...
'Style', 'pushbutton',...
'String', 'Get selected points index',...
'Position', [5, 5, 200, 30],...
'Units', 'pixels',...
'Callback', {#brushCallback, hLines, hAx} ...
);
brush(hFig, 'on');
end
function brushCallback(~, ~, hLines, hAx)
index_vec = cellfun(#logical, get(hLines,'BrushData'), 'UniformOutput', false);
plot(hAx(2), ...
hLines(1).XData(index_vec{1}), hLines(1).YData(index_vec{1}), ...
hLines(2).XData(index_vec{2}), hLines(2).YData(index_vec{2}), ...
hLines(3).XData(index_vec{3}), hLines(3).YData(index_vec{3}));
end

How to add extra information to figure's legend?

I want to add extra Information to a figure in MATLAB, like this:
Is it possible? (Of course I want it to be more beautiful)
I put together something reasonably generic, see below.
I will generalize this a bit more and post it to the File Exchange, I think it's a fairly nice tool to have around :)
I intend to
automatically adjust the table size to fit its contents
make it suited for arbitrary legend placement
couple the table and legend together when moving them with the mouse
make the header optional
support various types of input
But for now:
% Example input
plot(1,1,'r.', 1.1,1.1', 'b.', 1.2,1.2, 'k.');
legendHandle = legend('plot 1', 'plot 2 with longer title', 'plot 3');
tableHead = {'\theta_0' '\phi' 'df/dx'};
tableContent = rand(3);
% Extract information
legendPosition = get(legendHandle, 'position');
children = get(legendHandle, 'children');
labels = children(strcmp(get(children, 'type'), 'text'));
% Basic error traps
if size(tableContent,1) ~= numel(labels)
error('LegendTable:dimension_mismatch',...
'Each legend entry must have a corresponding row in the table.')
end
if size(tableHead,2) ~= size(tableContent,2)
error('LegendTable:dimension_mismatch',...
'Table header dimensions are inconsistent with table data.');
end
% Convert header & content to cell-array when necessary
if isnumeric(tableContent)
tableContent = cellfun(#num2str, ...
num2cell(tableContent), 'UniformOutput', false);
end
if isnumeric(tableHead)
tableHead = cellfun(#num2str, ...
num2cell(tableHead), 'UniformOutput', false);
end
% Proper tick locations for the table
xticks = linspace(0, 1, numel(tableHead)+1);
yticks = linspace(0, 1, numel(labels)+2);
% Text positions are in the centers of the table cells
txt_xPositions = xticks(1:end-1) + (xticks(2)-xticks(1))/2;
txt_yPositions = fliplr(yticks(1:end-1) + (yticks(2)-yticks(1))/2);
% Derive correct table position
headerHeight = legendPosition(4)/numel(labels);
tablePosition = legendPosition + [0 -headerHeight 0 headerHeight];
% Shift position of original legend
set(legendHandle, 'position', legendPosition + [-tablePosition(3) -headerHeight 0 0])
% Create table
table = axes(...
'position', tablePosition,...
'xtick', xticks,...
'ytick', yticks,...
'xticklabel', [],...
'yticklabel', [],...
'gridlinestyle', '-',...
'box', 'on',...
'tag', 'LegendTable');
grid on
% Print table header & table entries
kk = 1;
tableTexts = zeros(numel(tableHead)+numel(tableContent),1);
for ii = 1:numel(txt_xPositions)
% Column header
tableTexts(kk) = text(txt_xPositions(ii), txt_yPositions(1), tableHead{ii},...
'parent', table,...
'HorizontalAlignment', 'center');
kk = kk + 1;
% Column content
for jj = 1:numel(txt_yPositions)-1
tableTexts(kk) = text(...
txt_xPositions(ii), txt_yPositions(jj+1), tableContent{jj,ii},...
'parent', table,...
'HorizontalAlignment', 'center');
kk = kk + 1;
end
end
Result:
Well perhaps a bit of an overkill:
You could use uitable, like this
%define your data:
dat = {' green', 1, 2;...
' blue', 2,3.1;...
' yellow', 3, 4.8;}
columnname = {' ', 'Param1', 'Param2'};
columnformat = {'char', 'numeric', 'numeric'};
t = uitable('Units','normalized','Position',...
[0.05 0.05 0.755 0.87], 'Data', dat,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'RowName',[], 'Parent', gcf);
you just need to know the handle, if gcf (get current figure) doesnt work!
you can then resize and do whatever you want, to make it look nice...
You can use the num2str function to pass parameters to the legend.
legend(['red ' num2str(param1_red) ' ' num2str(param2_red)], ... )

Subplot label in matlab figures

I would like to give the subplots I make a simple label. Unfortunately I'm getting an ugly behavior. Consider the following function:
function h = set_label1(label)
tlh = get(gca, 'Title');
if strcmp(get(tlh, 'String'), '')
title(' ');
end
ylh = get(gca, 'YLabel');
if strcmp(get(ylh, 'String'), '')
ylabel(' ');
end
ylp = get(ylh, 'Position');
x = ylp(1);
tlp = get(tlh, 'Position');
y = tlp(2);
h = text('String', label, ...
'HorizontalAlignment', 'right',...
'VerticalAlignment', 'Baseline', ...
'FontUnits', 'pixels', ...
'FontSize', 16, ...
'FontWeight', 'bold', ...
'FontName', 'Arial', ...
'Position', [x y 0]);
end
Here is a simple test run:
figure;
h1 = axes('OuterPosition', [0,0,.5 1]);
set(h1,'LooseInset',get(h1,'TightInset'));
h2 = axes('OuterPosition', [.5,0,.5 1]);
set(h2,'LooseInset',get(h2,'TightInset'));
axes(h1);
plot([0 1], [4 5]);
set_label1('A');
axes(h2);
plot([0 1], [4 5]);
set_label1('B');
The picture I obtain is:
If you resize the figure the labels will not be in the right position anymore. That is fine, I expected it (If you know how to put them back where they belong and you tell us that would make me very happy).
THe problem I'm facing is that I do not want to specify the position of the label in 'data' units.
Instead, I want to use normalized units. So I used modified form of function. Now let us use this:
function h = set_label2(label)
tlh = get(gca, 'Title');
if strcmp(get(tlh, 'String'), '')
title(' ');
end
ylh = get(gca, 'YLabel');
if strcmp(get(ylh, 'String'), '')
ylabel(' ');
end
oldUnits = replace_prop(ylh, 'Units', 'normalized');
ylp = get(ylh, 'Position');
x = ylp(1);
set(ylh, 'Units', oldUnits);
oldUnits = replace_prop(tlh, 'Units', 'normalized');
tlp = get(tlh, 'Position');
y = tlp(2);
set(ylh, 'Units', oldUnits);
h = text('String', label, ...
'HorizontalAlignment', 'right',...
'VerticalAlignment', 'Baseline', ...
'FontUnits', 'pixels', ...
'FontSize', 16, ...
'FontWeight', 'bold', ...
'FontName', 'Arial', ...
'Units', 'normalized',...
'Position', [x y 0]);
end
function oldvalue = replace_prop(handle, propName, newvalue)
oldvalue = get(handle, propName);
set(handle, propName, newvalue);
end
Running the same test:
figure;
h1 = axes('OuterPosition', [0,0,.5 1]);
set(h1,'LooseInset',get(h1,'TightInset'));
h2 = axes('OuterPosition', [.5,0,.5 1]);
set(h2,'LooseInset',get(h2,'TightInset'));
axes(h1);
plot([0 1], [4 5]);
set_label2('A');
axes(h2);
plot([0 1], [4 5]);
set_label2('B');
We obtain the exact same picture as before. The only problem is that when we resize it now something bad happens:
The labels are actually in the correct position. But it seems that the 'LooseInset' and 'TightInset' property I used make the axes act as if there is no labels.
Is there any fix for this? Really all I am doing is getting the position of the title and ylabel in normalized units as opposed in data units and this seems to mess it up.
The reason I need to get it in normalized units is so that when we get a 3D plot I can position the label with respect to the title and the zlabel.
For posterity's sake here is the version I decided to go with. It does what I expect it to do, but now I have a problem which I have no idea how to solve. OK, first the good news, here is the function called axes_label.
function c = axes_label(varargin)
if isa(varargin{1}, 'char')
axesHandle = gca;
else
axesHandle = get(varargin{1}{1}, 'Parent');
end
if strcmp(get(get(axesHandle, 'Title'), 'String'), '')
title(axesHandle, ' ');
end
if strcmp(get(get(axesHandle, 'YLabel'), 'String'), '')
ylabel(axesHandle, ' ');
end
if strcmp(get(get(axesHandle, 'ZLabel'), 'String'), '')
zlabel(axesHandle, ' ');
end
if isa(varargin{1}, 'char')
label = varargin{1};
if nargin >=2
dx = varargin{2};
if nargin >= 3
dy = varargin{3};
else
dy = 0;
end
else
dx = 3;
dy = 3;
end
h = text('String', label, ...
'HorizontalAlignment', 'left',...
'VerticalAlignment', 'top', ...
'FontUnits', 'pixels', ...
'FontSize', 16, ...
'FontWeight', 'bold', ...
'FontName', 'Arial', ...
'Units', 'normalized');
el = addlistener(axesHandle, 'Position', 'PostSet', #(o, e) posChanged(o, e, h, dx, dy));
c = {h, el};
else
h = varargin{1}{1};
delete(varargin{1}{2});
if nargin >= 2
if isa(varargin{2}, 'char')
set(h, 'String', varargin{2});
if nargin >=3
dx = varargin{3};
dy = varargin{4};
else
dx = 3;
dy = 3;
end
else
dx = varargin{2};
dy = varargin{3};
end
else
error('Needs more arguments. Type help axes_label');
end
el = addlistener(axesHandle, 'Position', 'PostSet', #(o, e) posChanged(o, e, h, dx, dy));
c = {h, el};
end
posChanged(0, 0, h, dx, dy);
end
function posChanged(~, ~, h, dx, dy)
axh = get(h, 'Parent');
p = get(axh, 'Position');
o = get(axh, 'OuterPosition');
xp = (o(1)-p(1))/p(3);
yp = (o(2)-p(2)+o(4))/p(4);
set(h, 'Units', get(axh, 'Units'),'Position', [xp yp]);
set(h, 'Units', 'pixels');
p = get(h, 'Position');
set(h, 'Position', [p(1)+dx, p(2)+5-dy]);
set(h, 'Units', 'normalized');
end
Ok, so how do we use this crappy function? I made it so that we can have these uses:
% c = axes_label('label')
% Places the text object with the string 'label' on the upper-left
% corner of the current axes and returns a cell containing the handle
% of the text and an event listener.
%
% c = axes_label('label', dx, dy)
% Places the text object dx pixels from the left side of the axes
% and dy pixels from the top. These values are set to 3 by default.
%
% c = axes_label(c, ...)
% Peforms the operations mentioned above on cell c containing the
% handle of the text and the event listener.
%
% c = axes_label(c, dx, dy)
% Adjusts the current label to the specifed distance from the
% upper-left corner of the current axes.
If we perform the same test as before:
figure;
h1 = axes('OuterPosition', [0,0,.5 1]);
set(h1,'LooseInset',get(h1,'TightInset'));
h2 = axes('OuterPosition', [.5,0,.5 1]);
set(h2,'LooseInset',get(h2,'TightInset'));
axes(h1);
plot([0 1], [4 5]);
axes_label('A');
axes(h2);
plot([0 1], [4 5]);
axes_label('B', 250, 250);
Now we obtain what I wanted. Label 'A' is set at the upper-left corner of the axes's Outerbox. And label B I explicitly set it to be 250 pixels from its upper-left corner. Here is a plot:
What I like about this function is that if I were to store the cell returned from it and then I put back I can change the position. For instance if label = axes_label('A'); Then on the command prompt I can do label = axes_label(label, 10, 20); and I will see my label move.
The problem I'm facing now is ralated to the function export_fig
If I try to use this:
export_fig('testing.png', '-nocrop', '-painters');
Then this is the figure I obtain.
This is the reason why I exaggerated with label B. Before I added the event listeners export_fig would do an OK job at printing the labels where I had positioned them. But somehow now export_fig doesn't do what it claims it does. Mainly exporting an image with
Figure/axes reproduced as it appears on screen
If instead we remove the option -painters then we get this:
There is probably a bug in code since I'm not experienced with listeners, so if anyone can fix this behavior and/or can improve on this code please feel free to do so and share it as an answer.
First of all, I like your idea of using the title/y-label to position the text on the upper left corner, clever :)
Now, instead of using normalized units, keep using data units and create an event listener for whenever the title or the y-label change their positions, and use their new values to re-adjust the created text.
So add the following to the end of your set_label1 function:
addlistener(ylh, 'Position', 'PostSet', #(o,e) posChanged(o,e,h,1))
addlistener(tlh, 'Position', 'PostSet', #(o,e) posChanged(o,e,h,2))
and here is the callback function used for both cases (we use the last argument idx to control whether to set x or y coordinate):
function posChanged(src,evt,hTxt,idx)
posLabel = evt.NewValue; %# new position of either title/y-label
posText = get(hTxt, 'Position'); %# current text position
posText(idx) = posLabel(idx); %# update x or y position (based on idx)
set(hTxt, 'Position',posText) %# adjust the text position
end

MATLAB update waitbar

I am trying to implement a "percent complete" bar in a MATLAB program, using the waitbar function. However, I am having trouble with it. Here is the code that I have currently:
in my GUI
POSITION = [53.3333 20 188.5446 20];
H = uiwaitbar(POSITION);
for percentageDone = 0;
uiwaitbar(H,percentageDone);
end
then
function h = uiwaitbar(varargin)
if ishandle(varargin{1})
ax = varargin{1};
value = varargin{2};
p = get(ax,'Child');
x(3:4) = value;
set(p,'XData',x)
return
end
pos = varargin{1};
bg_color = [1 1 1];
fg_color = [0 .5 0];
h = axes('Units','pixels',...
'Position',pos,...
'XLim',[0 100],'YLim',[0 1],...
'XTick',[],'YTick',[],...
'Color',bg_color,...
'XColor',bg_color,'YColor',bg_color);
patch([0 0 0 0],[0 1 1 0],fg_color,...
'Parent',h,...
'EdgeColor','none',...
'EraseMode','none');
end
Elsewhere in the script, I have a KeyPressFcn callback, in which the user inputs the answer to their questions. At the end of this callback, for every correct answer I want the waitbar to fill up a little. However, no matter what values I assign to percentageDone variable the waitbar in the GUI does not change at all.
Can anybody help me with this?
I'm confused, you say you are using the builtin function WAITBAR, but then you seem to be implementing one yourself..
Anyway, here is a rather useless example that shows a custom progress bar. Just keep pressing "next" :)
function progressBarDemo()
%# a figure and a plot area
hFig = figure('Menubar','none');
hAxPlot = axes('Parent',hFig, 'Box','on', ...
'Units','normalized', 'Position',[0.1 0.2 0.8 0.6]);
hLine = line('Parent',hAxPlot, 'XData',1:1000, 'YData',nan(1,1000), ...
'Color','b');
%# next button
uicontrol('Style','pushbutton', 'String','next', ...
'Callback',#buttonCallback);
%# progress bar axis
x = linspace(0, 1, 13+1); %# steps
hAx = axes('Parent',hFig, 'XLim',[0 1], 'YLim',[0 1], ...
'XTick',[], 'YTick',[], 'Box','on', 'Layer','top', ...
'Units','normalized', 'Position',[0 0.9 1 0.1]);
hPatch = patch([0 0 x(1) x(1)], [0 1 1 0], 'r', 'Parent',hAx, ...
'FaceColor','r', 'EdgeColor','none');
hText = text(0.5, 0.5, sprintf('%.0f%%',x(1)*100), ...
'Parent',hAx, 'Color','w', 'BackgroundColor',[.9 .5 .5], ...
'HorizontalAlign','center', 'VerticalAlign','middle', ...
'FontSize',16, 'FontWeight','bold');
counter = 2;
%# next button callback function
function buttonCallback(src,evt)
%# draw another random plot
set(hLine, 'YData',cumsum(rand(1000,1)-0.5))
%# update progress bar
set(hPatch, 'XData',[0 0 x(counter) x(counter)])
set(hText, 'String',sprintf('%.0f%%',x(counter)*100))
%# terminate if we have reached 100%
counter = counter + 1;
if counter > numel(x)
set(src, 'Enable','off', 'String','Done')
return
end
end
end
You're probably just missing a drawnow call after setting the XData property, to force a flush of the graphics events queue. If this does not fix your problem, then include enough code to reproduce the symptoms.
Have you tried using Progressbar from the File Exchange? It might save you a lot of hassle. I've always had good results with it.
Do you first create the waitbar? Something like this:
h = waitbar(0, '1', 'Name', 'My progress bar', 'CreateCancelBtn', 'setappdata(gcbf, ''canceling'', 1)');
After that, to update the waitbar:
Edit: fixed bug with text output: percentageDone must be multiplied by 100.
waitbar(percentageDone, h, sprintf('Already %d percent ready!', 100*percentageDone));

MATLAB stop program after X repetitions?

I have this program, which as you can see is pulling random pictures out of a directory, and asking the user to compare them. After setting the value with the slider, the user presses a "Next Trial" button, which resets the slider and the random picture pair. How do I modify the code so that, after a certain number of repetitions (button presses), the program automatically ends (preferably with a "Experiment Ended" message)?
I can't find anything about how to do this in the MATLAB documentation. Do I need to set a variable, so that everytime the button is pressed "1" is added to the value of the variable, so that when it reaches a certain number (say "100") it terminates? Is that the easiest way to do this?
Here's the script:
function trials
files = dir(fullfile('samples','*.png'));
nFiles = numel(files);
combos = nchoosek(1:nFiles, 2);
index = combos(randperm(size(combos, 1)), :);
picture1 = files(index(1)).name;
picture2 = files(index(2)).name;
image1 = fullfile('samples',picture1);
image2 = fullfile('samples',picture2);
subplot(1,2,1); imshow(image1);
subplot(1,2,2); imshow(image2);
uicontrol('Style', 'text',...
'Position', [200 375 200 20],...
'String','How related are these pictures?');
uicontrol('Style', 'text',...
'Position', [50 375 100 20],...
'String','Unrelated');
uicontrol('Style', 'text',...
'Position', [450 375 100 20],...
'String','Closely related');
uicontrol('Style','pushbutton','String','Next Trial',...
'Position', [250 45 100 20],...
'Callback','clf; trials()');
h = uicontrol(gcf,...
'Style','slider',...
'Min' ,0,'Max',50, ...
'Position',[100 350 400 20], ...
'Value', 25,...
'SliderStep',[0.02 0.1], ...
'BackgroundColor',[0.8,0.8,0.8]);
set(gcf, 'WindowButtonMotionFcn', #cb);
lastVal = get(h, 'Value');
function cb(s,e)
if get(h, 'Value') ~= lastVal
lastVal = get(h, 'Value');
fprintf('Slider value: %f\n', lastVal);
end
end
end
One problem I see here is that the callback for your "Next Trial" button simply calls the function trials again. This is going to generate the combinations of images again, which you only want/need to do once. You should set the callback to be another nested function (like cb) so it can access the already-generated combinations.
Another problem is how you initialize picture1 and picture2. You should do your indexing like so:
picture1 = files(index(1,1)).name; %# Note that index is 2-dimensional!
picture2 = files(index(1,2)).name;
Now, you'll first want to initialize a variable to track the number of trials inside the function trials, as well as a maximum number of trials:
nReps = 1;
maxReps = 100;
Then your "Next Trial" button callback would look something like this:
function newTrial(s, e)
%# I assume you need the slider value for each trial, so fetch it
%# and save/store it here.
%# Check the number of trials:
if (nReps == maxReps)
close(gcf); %# Close the figure window
else
nReps = nReps + 1;
end
%# Get the new images:
picture1 = files(index(nReps, 1)).name;
picture2 = files(index(nReps, 2)).name;
image1 = fullfile('samples', picture1);
image2 = fullfile('samples', picture2);
%# Plot the new images:
subplot(1,2,1);
imshow(image1);
subplot(1,2,2);
imshow(image2);
%# Reset the slider to the default value:
set(h, 'Value', 25);
end
One additional suggestion... instead of displaying the slider value on the screen using FPRINTF, I would create a text object in your GUI and simply update its string value:
hText = uicontrol('Style', 'text', ...
'String', 'Slider value: 25', ... );
%# And in function cb...
set(hText, 'String', sprintf('Slider value: %f', lastVal));