How to control the time of displaying of a figure? - matlab

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)

Related

MATLAB show all 'active' plots, plt.show()?

hold on
ax = gca;
plot(ax, [1 2 3])
hold off
hold on
ax = gca;
plot(ax, [3 2 1])
hold off
Python's Matplotlib has plt.show() which can be used like
plt.plot([1, 2, 3])
plt.show()
plt.plot([3, 2, 1])
plt.show()
to display two separate figures. I've tried to recreate this behavior with the top block, with no success. Can it be done?
Context
I know this can be accomplished with explicit calls to figure and axes, but the idea is for this to happen after we finish plotting, not before we begin. That is, I define convenience plot functions
function plot1(varargin)
% do stuff
end
function plot2(varargin)
% do stuff
end
and each has a keyword argument show, which is to be used like
plot1(x, show=true)
plot2(x, show=true)
so plot2 should figure out that something's plotted before it without user input.
I tested below to work as intended with calls to variants of plot, scatter, and image. Also, as in Python, show=false for the last executed plot still acts same as show=true.
plot2([1 2 3], show=1)
plot2([3 2 1])
plot2([1 2 3])
plot2([3 2 1])
function plot2(x, C)
arguments
x;
C.show = false;
end
hold on
fig = gcf();
ax = gca();
plot(ax, x);
maybe_show(fig, ax, C.show)
end
function maybe_show(fig, ax, show_)
fig.Visible = true;
if show_
figure('Visible', 'off')
hold off
end
end

Plotting array of x and y values as points

I can't understand this: when I write the following code I obtain this graph in matlab.
x = [0 1 2 3 4 5];
y = [0 1 0 1 0 1];
figure
plot(x,y);
I had just expected that only the points written in arrays x and y would be plotted ,but the graph shows lines also...
I can't understand why is it so... Please help where am I wrong
Try to use the following
figure(10);
plot(x,y, '.');
figure(20);
plot(x,y, 'x');
figure(30);
plot(x,y, '-r');
See the differences... a dot-scatter, x-scatter and red line plot.
In the plot documentation you can read more about line styles. By default it is a blue line, as you can see in your plot!

Crossed stem plot in Matlab

I would like to reproduce in Matlab a plot that looks like this:
The stem3 plot command sounds nice but only for the vertical stems. Not the second series with the horizontal ones.
Everything would be easy if I could plot using the usual commands and rotate the result.
How a about this? Manually plot each line in 3D stemming from the x axis:
x = 0:.01:2*pi*3;
z = sin(x);
y = -sin(x);
hold on
for n = 1:numel(x);
plot3([x(n) x(n)], [0 y(n)], [0 0], 'r');
plot3([x(n) x(n)], [0 0], [0 z(n)], 'b');
end
view(15,25)
As noted by #TheMinion, it's easier to use fill3:
x = 0:.01:2*pi*3;
z = sin(x);
y = -sin(x);
fill3(x,y,zeros(size(x)),'r')
hold on
fill3(x,zeros(size(x)),z,'b')
view(15,25)

hide a range of color from colorbar

I have a map that contains values from 0 to 1 but also NaN contents.
I manage to defined a contour like map with the following code in MATLAB:
imagesc(map)
contourcmap('jet',[-0.3 0 0.3 0.6])
myMap = [[1 1 1]; ...
[1 0 0]; ...
[0 1 0]; ...
[0 0 1]];
colormap(myMap);
cbar = colorbar
what I get is a map like this:
however I'd like to show only red,green and blue on the colorbar and get rid of the white range (i.e., [-0.3,0)) but don't want to change the color scale on the main map. is this possible in matlab?
colorbar is an axes object and as such you can modify its properties, including the color limits. This is done through the 'YLim' property, like this:
colorbar('YLim', [0 0.6]);
Or you can modify this at any time using the object handle:
cbar = colorbar;
set(cbar, 'YLim', [0 0.6]);

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

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.