Matlab - how to draw pixels on a black full screen? - matlab

I want matlab to show me a full screen, all black, and to be able to set pixels on it.
Is it even possible?

You can't totally do that using pure MATLAB code. On Windows, I tried different combinations, but the taskbar will still be on top (the one with the Start button):
%# 1)
sz = get(0, 'ScreenSize');
figure('Menubar','none', 'WindowStyle','modal', ...
'Units','pixels', 'Position', [0 0 sz(3) sz(4)])
%# 2)
figure('Menubar','none', 'Units','normalized', 'Position',[0 0 1 1])
%# 3)
hFig = figure('Menubar','none', 'Units','normalized', 'Position',[0 0 1 1]);
set(hFig, 'Units','pixels')
p = get(hFig, 'Position');
set(hFig, 'Position', [1 31 p(3) p(4)-8]);
You would have to write a MEX function and call the the Win32 API directly. Fortunately, there should be existing submissions on FEX implementing such functionality.
Here is an example of creating a full screen figure, and drawing points with the mouse. I am using the WindowAPI solution by Jan Simon
%# open fullscreen figure
hFig = figure('Menubar','none');
WindowAPI(hFig, 'Position','full');
%# setup axis
axes('Color','k', 'XLim',[0 1], 'YLim',[0 1], ...
'Units','normalized', 'Position',[0 0 1 1], ...
'ButtonDownFcn',#onClick)
The callback function:
function onClick(hObj,ev)
%# draw point
p = get(hObj,'CurrentPoint');
line(p(1,1), p(1,2), 'Color','r', 'LineStyle','none', ...
'Marker','.', 'MarkerSize',40, 'Parent',hObj)
end

Check out psychophysics toolbox. It does exactly what you are looking for and more.

Try this:
screen_size = get(0, 'ScreenSize');
buff=zeros(screen_size(3),screen_size(4));
for i=1:50
buff(screen_size(3)/2-i,screen_size(4)/2+i)=100;
end
f1 = image(buff)
colormap(gray)
set(gcf,'windowstyle','modal');
set(gcf,'OuterPosition', screen_size);
set(gcf,'position',screen_size);
set(gcf,'Units','normal', 'outerposition',[0 0 1 1])
set(gca,'Visible', 'Off', 'Position',[0 0 1 1])
Use Alt+F4 (or equivalent) to kill the window. I don't fully understand why you have to do it this way, but it is the only way I've ever found to remove the window frame and make the plot extend full screen.

Related

Cannot place an image just above a bar plot in matlab [duplicate]

This question already has answers here:
How can I modify my subplot positions to stop them overwriting each other?
(2 answers)
Closed 2 years ago.
I'm trying to print an image above a bar plot in the same figure, manually changing their position using the set command with the OuterPosition option.
This works only if the vertical coordinate of the image is at least 0.42, in fact if it is below this value then the image disappears.
How can I place the image below 0.42?
subplot(2,1,1)
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
set(gca, 'OuterPosition', [.3 .42 .4 .4]);
subplot(2,1,2)
bar(1:10)
set(gca, 'OuterPosition', [.3 0 .35 .23]);
Assuming that your MATLAB version is recent (>= R2019b), you could use tiledlayout instead of subplot:
tiledlayout(2,1)
nexttile
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
nexttile
bar(1:10)
which will place the map above the chart.
If you wish for the map to be directly above the chart, you will have to modify the dimensions manually as follows:
pos1 = [0.2 0.1 0.6 0.3];
subplot('Position',pos1)
bar(1:10)
pos2 = [0.2 0.41 0.6 0.3];
subplot('Position',pos2)
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
The code you display does not match the animated image you link so we cannot know what is wrong in the code you actually used.
Setting the position of an axes is normally as straightforward as you were doing it. Worst case is the axes can move outside of visible area or be hidden behind other object. Consider the following example, the image never magically disapear, it just move where I tell it to:
function demo_move_image
% The code as provided -----------------------------------------------
ax1 = subplot(2,1,1) ;
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
set(ax1, 'OuterPosition', [.3 .42 .4 .4]);
ax2 = subplot(2,1,2) ;
bar(1:10)
set(ax2, 'OuterPosition', [.3 0 .35 .23]);
% END - The code as provided -----------------------------------------
% Make a slider to control the vertical position of the image
uisld = uicontrol('Style','slider','Min',0,'Max',1,'Value',.42,...
'Unit','norm','Position',[.95 0 .05 1],'Callback',#slidercb) ;
title(ax1,num2str(get(uisld,'Value')))
% Callback for the slider
function slidercb(~,~)
set(ax1, 'OuterPosition', [.3 get(uisld,'Value') .4 .4])
title(ax1,num2str(get(uisld,'Value')))
end
end
Due to some reason, the subplot() command deletes the axes behind it, so it is better to use axes().
ax1 = axes('OuterPosition', [.3 .35 .4 .4]);
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
ax2 = axes('OuterPosition', [.3 0 .35 .23]);
bar(1:10)

How to adjust figure size when using plotyy?

I'm trying to adjust the figure size when using plotyy:
clc;
clear all;
t = 0:.1:4*pi;
y = sin(t);
figure(1);
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 4 2]);
plot(t,y)
xlabel('Time(s)')
ylabel('y(t)')
title('Sin function')
legend('y=sin(t)')
axis([0 t(end) -1.5 1.5])
set(gca,...
'Units','normalized',...
'YTick',-1.5:.5:1.5,...
'XTick',0:t(end)/4:t(end),...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times')
set(gca, 'Position', get(gca, 'OuterPosition') - ...
get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);
figure(2);
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 4 2]);
[haxes,hline1,hline2]=plotyy(t,y,t,t);
ylabel(haxes(1),'sin(t)')
ylabel(haxes(2),'45degree')
xlabel(haxes(1),'Time(s)')
title('Sin function')
set(haxes,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times')
set(haxes(1), 'Position', get(haxes(1), 'OuterPosition') - ...
get(haxes(1), 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 0 0; 0 0 0 1]-...
get(haxes(2), 'TightInset') * [0 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 0]);
The example indicates that the procedure works well for plot but not for plotyy. It seems that when I use plot, TightInset takes into account the text on the bottom and on the top (as it should), but it doesn't take them into account when I use plotyy. But I don't understand why this is the case and how to fix it. Any ideas?
[Code below is related to my comment to the chosen answer]
figure(3);
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 4 2]);
plot(t,y,'b');
set(gca,'YColor','b')
xlabel('Time(s)')
ylabel('y(t)')
title('Sin function')
axis([0 t(end) -1.5 1.5]);
set(gca,...
'Units','normalized',...
'YTick',-1.5:.5:1.5,...
'XTick',0:t(end)/4:t(end),...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times')
axesPosition = get(gca,'Position');
hNewAxes = axes('Position',axesPosition,...
'Color','none',...
'YLim',[0 10],...
'YAxisLocation','right',...
'XTick',[],...
'Box','off');
set(gca,'YColor',[0 .5 0]);
ylabel(hNewAxes,'45degree');
hold all
plot(hNewAxes,t,t,'color',[0 .5 0]);
hold off
Personally I try to avoid plotyy because there is always a property which doesn't behave as expected. If I need I usually create the second axes and set the properties by hand, copying what I need from the first axes of course, and linking a few properties.
If you look at the code for plotyy you will see that it does the same ... with some extra.
A nice extra is how it tries to match the ticks on both y axes, even when their span is different (but you could copy that part out and still use a manual dual axes solution).
Another extra, and what I suspect is responsible for the behavior you observed, is that it sets listeners and callback to readjust the position properties of both axes. Somewhere down the line it must kill some automatic readjustment of the axes because if you notice in your code, even before you tried to change the position manually, the axes were not readjusted when you called xlabel and title (as they are normally when you do that on a standard single axes).
This is why the TightInset you try to use to readjust your axes does not work ... because the axes were not readjusted when you added the text objects, (and so the vertical TightInset is wrong).
Now for your case, fortunately this only seem to affect the vertical part of the TightInset, so a quick workaround to get everything tight as in your first figure is to use the OuterPosition property for the vertical part (after getting the margin from TightInset for the horizontal part).
So in your code just replace your last call to set(haxes,'Position', ...) with:
margin = get(haxes(1),'TightInset') * [0;0;1;0] ;
set(haxes,'OuterPosition',[-margin 0 1+margin 1])
and everything should be tight again ;-)

How to control the time of displaying of a figure?

Now, I am trying to use Matlab for simulating a motion. Here is a part of the program:
L=line(x, y, 'color','k','erasemode','xor');
rotate(L,[0 0 1],90,[0 0 0]);
Here is my problem: I want the line to show after the rotate command, not the line function, what functions I should use to obtain the desired results?
Try this:
L = line([1 2],[3 4], 'color','k', 'visible', 'off');
rotate(L,[0 0 1],90,[0 0 0]);
set(L, 'visible', 'on');
(I replaced x and y by some random numbers to make the code work)

Adding text to a figure - 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.

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));