plotting from cell array of different lengths - matlab

this might come as an eco in this forum but I couldn't find a solution that I could apply to my problem. I have a cell of res_A1 of size (1x500) and in each cell there is a vector (1xlength) where length varies. I would like to plot in the same graph every vector or maybe a handful of them. All lines can be in the same color. I have tried following but the graph make no sense
data=res_A1(1,:,end);
plot(cell2mat(data)');
Also I would like to plot the average of the 500 vectors, preferably this should be in the same graph in another color. Is there a nice way of doing this?

You can use cat to combine the vectors along the first dimension. Then you can pass the transpose of this matrix to plot and each column will be plotted as it's own plot.
plot(cat(1, data{:}).');
If we create some example data, this will yield.
data = arrayfun(#(x)rand(1,10), ones(1, 5), 'uni', 0);
plot(cat(1, data{:}).');
If you want specific ones (i.e. [1 3 5]), you can replace : above with the indices of the ones you want.
plot(cat(1, data{[1 3 5]}).');
If you want to plot the average, simply use mean on the result of the call to cat.
avg = mean(cat(1, data{:}), 1);
plot(avg);
And if you wanted it in the same plot:
alldata = cat(1, data{:});
avg = mean(alldata, 1);
% Plot all of the curves
plot(alldata.');
hold on
% Plot the average curve
plot(avg, 'LineWidth', 3, 'Color', [0.5 0.5 0.5], 'LineStyle', '--')
Update
If your data is all different lengths, You have two options, you could plot everything with a loop.
hax = axes;
hold(hax, 'on');
for k = 1:numel(data)
plot(data{k}, 'Parent', hax);
end
Or you could still try to combine everything into one matrix, padding with NaN values.
% Find the longest vector length
maxlength = max(cellfun(#(x)numel(x), data));
alldata = nan(maxlength, numel(data));
for k = 1:numel(data)
alldata(1:numel(data{k}),k) = data{k};
end
Then you can plot this and take the mean using nanmean.
plot(alldata);
avg = nanmean(alldata, 2);

Related

Plot rows of a 2d matrix in matlab

I have a 2d matrix A (100 x 100) where each row contains a signal to be plot.
I want to plot all of the signals in the same figure with different color for each row. How can I do this easily?
If you actually look at the documentation for plot you'll see that if you pass it a matrix, it will plot each column as a separate plot object on the same axes. As such you can simply pass the transpose of your data to plot.
% Example data
A = magic(10);
% Create a plot for each row
hplot = plot(A.');
This will plot each signal using the next plot color.
If you want to ensure that you have all different colors, you can use a colormap (such as parula) to explicitly set a different color per plot.
set(hplot, {'Color'}, num2cell(parula(size(A, 1)), 2))
Update
If you want to label your plots you could simply use legend to do this.
displaynames = arrayfun(#(x)sprintf('Plot %d', x), 1:size(A, 1), 'uni', 0);
set(hplot, {'DisplayName'}, displaynames.');
legend(hplot)
Or if you have too many plots to reasonably fit within a legend you can create an interactive plot that highlights a given plot when you mouseover it. Here is an example of such a thing.
htitle = title('');
set(gcf, 'WindowButtonMotionFcn', #(s,e)motionCallback(hittest(s)))
motionCallback(hplot(1));
function motionCallback(plt)
% Don't do anything if not a line object
[tf, ind] = ismember(plt, hplot);
if ~tf; return; end
set(hplot, 'linewidth', 1)
set(plt, 'LineWidth', 3)
set(htitle, 'String', sprintf('SelectedPlot: %d', ind))
drawnow
end
And the result

Is it possible to plot rows of a matrix without a for loop?

I have a matrix that stores multiple functions in its rows each evaluated against the interval [0,20]. I'm running through a for loop to output them at the moment. Is there a better way of doing this or is this the only way of doing it in MATLAB?
h = 0.1;
xLine = 0:h:20;
nGrid = length(xLine);
nu = [ 1, 2, 3 ];
nNu = length(nu);
b = zeros(nNu,nGrid);
for i=1:nNu
b(i:i,1:nGrid) = besselj(nu(i), xLine);
end
hFig = figure(1);
hold on
set(hFig, 'Position', [1000 600 800 500]);
for i=1:nNu
plot(xLine, b(i:i,1:nGrid))
end
You can use plot vectorized. Specifically, you can supply b directly into plot but you need to make sure that the larger of the two dimensions in b matches the total number of elements in the vector xLine. This is what you have, so we're good. Therefore, because each unique signal occupies a row in your matrix, just supply b into your plot call and use it a single time.
hFig = figure(1);
hold on
set(hFig, 'Position', [1000 600 800 500]);
plot(xLine, b);
This will plot each row as a separate colour. If you tried doing this, you'll see that the plots are the same in comparison to the for loop approach.
Check out the documentation for plot for more details: http://www.mathworks.com/help/matlab/ref/plot.html
Replace for loop with:
plot(xLine, b(:,1:nGrid))
Note: I can't perfectly recall but some older versions of Matlab may want everything in columns and you'd want to transpose the matrices:
plot(xLine.', b(:,1:nGrid).')

matlab plotting a family of functions

Generate a plot showing the graphs of
y=(2*a+1)*exp(-x)-(a+1)*exp(2*x)
in the range x ∈ <-2, 4> for all integer values of a between -3 and 3
I know how to make typical plot for 2 values and set a range on the axes, but how to draw the graph dependent on the parameter a?
To elaborate on Ben Voigt's comment: A more advanced technique would be to replace the for-loop with a call to bsxfun to generate a matrix of evaluations of M(i,j) = f(x(i),a(j)) and call plot with this matrix. Matlab will then use the columns of the matrix and plot each column with individual colors.
%%// Create a function handle of your function
f = #(x,a) (2*a+1)*exp(-x)-(a+1)*exp(2*x);
%%// Plot the data
x = linspace(-2, 4);
as = -3:3;
plot(x, bsxfun(f,x(:),as));
%%// Add a legend
legendTexts = arrayfun(#(a) sprintf('a == %d', a), as, 'uni', 0);
legend(legendTexts, 'Location', 'best');
You could also create the evaluation matrix using ndgrid, which explicitly returns all combinations of the values of x and as. Here you have to pay closer attention on properly vectorizing the code. (We were lucky that the bsxfun approach worked without having to change the original f.)
f = #(x,a) (2*a+1).*exp(-x)-(a+1).*exp(2*x); %// Note the added dots.
[X,As] = ndgrid(x,as);
plot(x, f(X,As))
However for starters, you should get familiar with loops.
You can do it using a simple for loop as follows. You basically loop through each value of a and plot the corresponding y function.
clear
clc
close all
x = -2:4;
%// Define a
a = -3:3;
%// Counter for legend
p = 1;
LegendText = cell(1,numel(a));
figure;
hold on %// Important to keep all the lines on the same plot.
for k = a
CurrColor = rand(1,3);
y= (2*k+1).*exp(-x)-(k+1).*exp(2.*x);
plot(x,y,'Color',CurrColor);
%// Text for legend
LegendText{p} = sprintf('a equals %d',k);
p = p+1;
end
legend(LegendText,'Location','best')
Which gives something like this:
You can customize the graph as you like. Hope that helps get you started!

Easy way to filter Infs in a scatter3 plot

Given the following code, how would one make the Inf values invisible in the scatter plot without color manipulation?
J = rand(20, 40, 5);
J(J>.6 & J<.4) = Inf;
% Plot a scatter matrix
shape = size(J);
[x,y,z] = meshgrid(1:shape(1), 1:shape(2), 1:shape(3));
scatter3(x(:), y(:), z(:), 4, J(:), 'fill');
Data that have NaN values are made invisible when plotting with MATLAB, which you can exploit in your case. Since you want to make the Infinte values as invisible, you can convert all those to NaNs and then plot them. Here you can take help of logical indexing to index into Inf element positions. Thus, the code would be -
J(isinf(J))=NaN
%// ... Plot J
One method could be to change the values higher than a certain threshold to NaN (or any other number). I believe NaN values will not show up in your scatter plot. You can do this with the same code you are already using.
J(J>10^6) = NaN;

draw a line of best fit through data with shaded region for error

I have the following data:
dat = [9.3,0.6,0.4,0.7;...
3.2,1.2,0.7,1.9;...
3.9,1.8,0.7,1.9;...
1.0,7.4,5.6,10.7;...
4.7,1.0,0.5,1.3;...
2.2,2.6,1.2,2.7;...
7.2,1.0,0.5,1.1;...
1.0,4.8,7.5,10.3;...
2.7,1.8,1.7,4.0;...
8.2,0.8,0.4,0.9;...
1.0,4.9,5.7,8.2;...
12.9,1.3,0.6,1.6;...
7.7,0.8,0.5,1.3;...
5.8,0.9,0.6,1.9;...
1.1,4.5,6.2,12.1;...
1.1,4.5,2.8,4.8;...
16.4,0.3,0.3,0.5;...
10.4,0.6,0.3,0.7;...
2.2,3.1,2.2,4.6];
where the first column shows the observed values the second column shows the modeled values and the third and fourth columns show the min and max respectively.
I can plot the relationship between observed and modeled by
scatter(d(:,1),d(:,2))
Next, I would like to draw a smooth line through these points to show the relationship. How can this be done? Obviously a simple straight line would not be much use here.
Secondly, I would like to use the min and max (3rd and 4th columns respectively) to draw a shaded region around the modeled values in order to show the associated error.
I would eventually like to have something that looks like
http://www.giss.nasa.gov/research/briefs/rosenzweig_03/figure2.gif
Something like this?
%// Rename and sort your data
[x,I] = sort(dat(:,1));
y = dat(I,2);
mins = dat(I,3);
maxs = dat(I,4);
%// Assume a model of the form y = A + B/x. Solve for A and B
%// using least squares
P = [ones(size(x)) 1./x] \ y;
%// Initialize figure
figure(1), clf, hold on
set(1, 'Renderer', 'OpenGl');
%// Plot "shaded" areas
fill([x; flipud(x)], [mins; flipud(maxs)], 'y',...
'FaceAlpha', 0.2,...
'EdgeColor', 'r');
%// Plot data and fit
legendEntry(1) = plot(x, P(1) + P(2)./x, 'b',...
'LineWidth', 2);
legendEntry(2) = plot(dat(:,1), dat(:,2), 'r.',...
'Markersize', 15);