I have 3D matrix of dimensions D x D x N. I want to create a dynamic heatmap to show how it's varying over N. Here's the MATLAB code I used to achieve this.
for n=1:N
heatmap(dynamicCov(:,:,n));
pause(0.5);
end
The issue with this code is that for each n, it opens a new figure window. I want it to be updated in the same Heatmap window. Is it possible to do that? Is there any other way to achieve this?
Thanks.
You need to use the undocumented 2nd input to HeatMap that indicates whether a plot should be created or not, and a few other Handle Graphics tricks to get the handle to the figure that is created. Something like
data = rand(20,20,10); % create test data
hmo = HeatMap(data(:,:,1),false); % create but do not plot
plot(hmo); % do the plot
allHFig = findall(0,'Type','figure'); % get handle to all open figures
hFig = allHFig(1); % we want the most recently created figure
for idx = 2:size(data,3)
hmo = HeatMap(data(:,:,idx),false); % create heatmap but do not plot
plot(hmo,hFig); % plot to our existing figure
pause(0.5);
end
I found a better and a lot simpler way of doing this. It uses built in imagesc() function instead of HeatMap() function from Bioinformatics toolbox. The code is as follows:
dynamicCov = rand(20,20,10); % create test data
N = size(dynamicCov,3);
for n=1:N
imagesc(dynamicCov(:,:,n));
colormap('copper');
colorbar;
pause(0.5);
end
Reference: http://buli.waw.pl/matlab-heatmap-colormap/
Related
I am taking a circuits class and for lab we need to do a little work with MATLAB to plot some of the results. I got the following code which I used to generate a Bode plot of the transfer function for a filter we were designing. I sort of get how it works but I don't really know or use MATLAB outside of this class.
clc;
close all
s=tf('s');
w=628*1000;
H=(1/(1 + 1.85*s/w + s^2/w^2))*(1/(1 + 0.77*s/w + s^2/w^2));
figure;
bode(H)
This worked fine but now I need to plot the transfer function I measured in the lab against this data on the SAME plot axis. How can I plot both of them together. I have the lab data as a list of gain frequency pairs.
Instead of bode(H), try:
[mag,ph,w] = bode(H); % gets the data without generating the figure
plot(w, mag, 'b'); % plots only the magnitudes
freqs = data(:,1); % These 2 lines depend on how your data is formatted
gains = data(:,2); % These 2 lines depend on how your data is formatted
hold on % will add new content to the existing figure without erasing the previous content
plot(freqs, gains, 'r');
hold off
you could also try (inspired by http://www.mathworks.com/help/ident/ref/bodeplot.html) :
h = bodeplot(H);
setoptions(h,'FreqUnits','Hz','PhaseVisible','off'); % suppress the phase plot
freqs = data(:,1); % These 2 lines depend on how your data is formatted
gains = data(:,2); % These 2 lines depend on how your data is formatted
hold on % will add new content to the existing figure without erasing the previous content
plot(freqs, gains, 'r');
hold off
I have a simulation running on Simulink and output signals change during simulation. I want to plot them at every step. What I can do is to use to Workspace blocks to transfer them to Matlab, but then I can only plot after the simulation finishes. I would like to plot the value at every instant of the simulation.
What I tried:
Create a figure in advance as: figure(1) and plot a static graph on it. Then I use
Matlab function inside Simulink :
function fcn(x,y)
coder.extrinsic('plot')
plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background')
Where x and y are my signals as input to matlab function block. However this results in plotting x and y in every timestep, but I would like to plot only the last value of the signal on the figure and delete the previous ones, in other words refresh the plot so that it is going to act as an animation. How can I achieve that? Thanks in advance
I think your code should work, with a few minor modifications:
I would do the following if I were you:
In the model callbacks, define your figure in the InitFcn callback:
fig_h = figure;
ax_h = axes;
set(ax_h,'Xlim',[0 12],'YLim',[0 12]) % or whatever axes limits you want
Then in your MATLAB Function block:
function fcn(x,y)
%#codegen
coder.extrinsic('plot')
plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background')
set(gca,'XLim',[0 12],'Ylim',[0 12]) % or whatever axes limits you want
You need little more elaborate function than just calling plot to animate your data. You should create a plot_fcn and make that function extrinsic. An example implementation of plot_fcn assuming scalar inputs with range 0 to 100 is
function plot_fcn(x,y)
persistent f h
if isempty(f)
f = figure;
h = plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background');
axis([0 100 0 100]);
axis manual
end
figure(f);
set(h, 'XData', x);
set(h, 'YData', y);
You can then call this function as
function fcn(x,y)
coder.extrinsic('plot_fcn')
plot_fcn(x,y);
Also checkout other questions regarding animation in MATLAB plots.
Hello i am having some problems understading basic plotting in matlab.
I can understand why you would use a for loop when plotting data?
Can anybody explain this to me?
I am making a simple linear plot. Is there any reason this should be inside a loop
If you are making a simple plot there is virtually no reason to use a loop.
If you check doc plot you will find that plot can take some vectors as input, or even matrices for more interesting situations.
Example:
x=0:0.01:1;
y=sin(x);
plot(x,y)
No there is no need in Matlab to use a for loop for plotting. If you are looking for a simple linear plot your code could look like this:
x=1:100;
y=3*x+4;
plot(x,y)
As you see there is no for loop needed. Same goes for nearly all plots and visualization.
A possible reason to use a for loop to plot thing may be having several data to plot in a single matrix. Say you have two matrix Ax (MxN) and Ay (MxN) where N the length of each data and M is the amount of different data wanted to plot. For example like in this case N is 201 and M is 3:
% Create Ax and Ay
Ax=meshgrid(0:0.1:20,1:3);
Ay=zeros(size(Ax));
% Sinusoidals with different frequencies
for k=1:3
Ay(k,:)=sin(k.*Ax(k,:));
end
% create colours
colorVec = hsv(3);
% Plot
hold on
for k=1:3
plot(Ax(k,:),Ay(k,:),'Color',colorVec(k,:))
end
hold off
You get:
Ive saved a figure as a .fig-file in MATLAB which I did now reopen after some time.
Is there a way to access the data which is saved in the Histogram? I want to replot it by using the hist() command instead of imhist into a new figure (the reason is that matlab2tikz cant export the histogram plotted by imhist properly).
I imagine I could access the data when I would know the handle of the histogram, right?
EDIT:
A = findall(gcf,'type','axes');
then inspecting
get(A(i))
to see which axes the histogram is plotted in. This works but I have to figure out how to retrieve the actual data.
But I somehow assume that I have to look at a parent/children of the axes handle (depending which hierarchy MATLAB creates of objects).
Okay I figured it out finally.
As written in my edit above, you can use findall to find the handles of all axes-objects.
After using it, try to find out which handle refers to which axes by looking at the entries like X/YLim in get(A(i)), after finding the axes-ID and storing it (the k-th element in A) to idx = A(K), use this script to read the entries from the histogram plotted by imhist() -> The values are replicated as often as described by the bins (YData) and then replotted by hist into a new figure:
% ----------------------------------------------------------------------- %
b = get(idx);
b = get(b.Children); % Get the Plot-Handle
x = b.XData; % Bins
y = b.YData; % Bin-Counts
data = [];
for i = 1:length(x)
data = [data x(i)*ones(1,y(i))]; % replicate data
end
figure
hist(data, length(unique(x)));
xlim([min(data) max(data)]);
Edit: The for-loop is a quick and dirty one ;-) Im sure there's a nicer solution e.g. by using repmat, but I was only interested in a quick solution :-)
i have a function that gets few parameters and plots a graph in Matlab
i want to use the function to plot few graphs with different parameters in one window.
i tried "subplot" but it doesn't do it.
is there a way doing it?
thanks
Use the command hold on and in the end hold off
Further information: http://www.mathworks.ch/ch/help/matlab/ref/hold.html
I would like to add one more thing in C. Colden suggestion, Provide the same x values for all plot as shown below:
x = 0:0.01:pi ;
y1 = sind(x) ;
y2= codd(x) ;
plot (x,y1)
hold on
plot(x,y2)
if you want to provide x1 and x2 for x values, then you will encounter with an error.