Consider that I have the following sets of values,
Y = [1 23 4 67 89 23 5 12 ]
X = [0 2 4 6 10 14 18 22 ]
I can draw a plot with the basic plot function. Now, from this plot, I want to know what the Y value for X=5. How can I get this data?
You can see, it's not part of my plot points, but something I was to extract from the graph.
Any ideas folks? I'm a bit surprised that there isn't a built in function for this.
The function you are looking for is interp1
For your example, the code would be
y = interp1(X, Y, 5, 'linear');
Since plot uses linear interpolation, you should use it as well.
y = interp1(X,Y,5);
Related
I have a discrete signal x of length N traced in MATLAB using the command
stem(abs(x)); axis([0 N+6 0 4]);
The resulted figure is shown below:
My question I need only some values corresponding for example to index [7 10 11 12 15 18 48 50 52 60] to be colored with different color , let's say with red.
How can I do that into my figure ?
Using Multiple Plots by hold on and Matrix Indexing
You could possibly and alternatively place a plot on top of plot by using hold on. This does require an adjustment where you need a vector in this case Sample and Indices which specify the sample number/data point index. You can also use matrix indexing to get the amplitude/data points corresponding to the key point, Indicies.
%Vector relating to the sample/data point number%
Sample = linspace(1,70,70);
%Random test data%
X = randi([0,2],1,70);
stem(Sample,X);
hold on
%Key indices to change colour%
Key_Indices = [7 10 11 12 15 18 48 50 52 60];
%Matrix indexing to get values/amplitudes corresponding to key indices%
X_Prime = X(Key_Indices);
stem(Key_Indices,X_Prime,'r');
axis([0 70 0 3]);
hold off
Ran using MATLAB R2019b
This code makes just the circles red and not the stems
plot with selected red circles
%Vector relating to the sample/data point number
Sample = linspace(1,70,70);
%Random test data
X = randi([0,2],1,70);
stem(Sample,X);
%Key indices to change color
Key_Indices = [7 10 11 12 15 18 48 50 52 60];
line(Sample(Key_Indices), X(Key_Indices), 'linestyle', 'none', 'marker', 'o', 'color', 'r')
axis([0 70 0 3])
grid on
I have made a matrix containing 13 different vectors with ~300K+ rows. I have visualized the matrix by transposing it and using the imagesc function to see the distribution of colors. All vectors have been resampled, processed and normalized between 0 & 1 individually.
The imagesc plot gives me this result (fig 1):
But, when I use the axis functionality to add x & y limits, I get this:
How do I maintain the imagesc plot while being able to add custom ticks and labels to the X & y axis? The x axis represents time, while the y axis will get its own labels with sensor names.
You redefine limits from 0 to 30 on the x-axis while the initial xlimits goes up to 3e5. Same issue with the y-axis
Here's how to redefine the Y-axis to put sensor names:
C = [0 2 4 6 9 ; 8 10 12 44 14; 16 48 10 32 23];
image(C)
% Get axis handle
ax = gca;
% Set where ticks will be
ax.YTick = [1 2 3];
% Set TickLabels;
ax.YTickLabel = {'S1','S2','S3'};
Figure out the ax.YTick where you want the labels to appear.
If you want the x-axis to go from 0 to 30, divide the x component of all vectors by 1e4 before plotting. Alternatively, you can add the line:
ax.XTickLabel = ax.XTick/1e4;
I have a sequence of points i wish to plot, but the resultant curve could be broken at places if the points are too far apart.
So in the 1D CASE :
1 2 3 7 9 11 12 16 18 19
Would be like :
1-2-3 7-9-11-12 16-18-19
or : seq1 seq2 seq3
I want to plot my sequence as discrete parts seq1 seq2 and seq3 which are not connected.
I am not too sure how to go about this
sea the code snippet below for a solution of your problem. I tried to explain as much in the code as possible, but don't hesitate to ask, if anything is not clear.
% constants, thresold defintion
T = 4;
% your data
a = [1 2 3 7 9 11 12 16 18 19 24 25 26 28 35 37 38 39];
% preparing the x-axis
x = 1:length(a);
% Getting the differences between the values
d = diff(a);
% find the suggested "jumps/gaps", greater/equal than the threshold
ind = find(d>=T);
figure;
hold on;
% Plotting the first part of a
y = nan*ones(1,length(a));
y(1:ind(1)) = a(1:ind(1));
plot(x,y);
% Plotting all parts in between: go through all found gaps
% and plot the corresponding values of "a" between them
for j=2:length(ind)
y = nan*ones(1,length(a));
y(ind(j-1)+1:ind(j)) = a(ind(j-1)+1:ind(j));
plot(x,y);
end;
% Plotting the last part of a
y = nan*ones(1,length(a));
y(ind(j)+1:end) = a(ind(j)+1:end);
plot(x,y);
The plot in MATLAB looks like this:
The code to generate this is very simple:
y = [0 18 450];
x = [0 5.3 6.575];
plot(x,y);
How could I know the values of 119 equally spaced discrete points on this plot?
In simple MATLAB plots, the points are connected together by simple linear interpolation. Simply put, a straight line is drawn between each pair of points. You can't physically get these points from the graph other than those you used to plot the points (at least not easily...).
If you however do desire 119 points at equally spaced intervals that would theoretically be obtained from the above set of 4 points, you can use the interp1 function to do so:
y = [0 18 450];
x = [0 5.3 6.575]
yy = interp1(x, y, linspace(min(x),max(x),119), 'linear');
interp1 performs linear (note the 'linear' flag at the end...) interpolation given a set of key points defined by x and y points and a set of x points to use to interpolate between the key x points to generate the interpolated y points stored in yy. linspace in this case generates a linearly increasing array from the smallest value in x to the largest value in x with 119 of these points.
Here's a running example with your data:
>> format compact;
>> y = [0 18 450];
>> x = [0 5.3 6.575];
>> yy = interp1(x, y, linspace(min(x),max(x),119), 'linear');
>> yy
yy =
Columns 1 through 8
0 0.1892 0.3785 0.5677 0.7570 0.9462 1.1354 1.3247
Columns 9 through 16
1.5139 1.7031 1.8924 2.0816 2.2709 2.4601 2.6493 2.8386
Columns 17 through 24
3.0278 3.2171 3.4063 3.5955 3.7848 3.9740 4.1633 4.3525
Columns 25 through 32
4.5417 4.7310 4.9202 5.1094 5.2987 5.4879 5.6772 5.8664
Columns 33 through 40
6.0556 6.2449 6.4341 6.6234 6.8126 7.0018 7.1911 7.3803
Columns 41 through 48
7.5696 7.7588 7.9480 8.1373 8.3265 8.5157 8.7050 8.8942
Columns 49 through 56
9.0835 9.2727 9.4619 9.6512 9.8404 10.0297 10.2189 10.4081
Columns 57 through 64
10.5974 10.7866 10.9759 11.1651 11.3543 11.5436 11.7328 11.9220
Columns 65 through 72
12.1113 12.3005 12.4898 12.6790 12.8682 13.0575 13.2467 13.4360
Columns 73 through 80
13.6252 13.8144 14.0037 14.1929 14.3822 14.5714 14.7606 14.9499
Columns 81 through 88
15.1391 15.3283 15.5176 15.7068 15.8961 16.0853 16.2745 16.4638
Columns 89 through 96
16.6530 16.8423 17.0315 17.2207 17.4100 17.5992 17.7885 17.9777
Columns 97 through 104
34.6540 53.5334 72.4128 91.2921 110.1715 129.0508 147.9302 166.8096
Columns 105 through 112
185.6889 204.5683 223.4477 242.3270 261.2064 280.0857 298.9651 317.8445
Columns 113 through 119
336.7238 355.6032 374.4826 393.3619 412.2413 431.1206 450.0000
nodes = [0 1 2 3 4 4 3 7 7 2 10 11 11 10 14 14 1 17 18 19 19 18 22 22 17 25 26 26 25 29 29]; This is the standard plot for a depth 4 tree. It is in a loop, and gets drawn 10 times. Now, each of these 10 times some numerical computations are done and different numbers are come up with. These numbers all point to some word tags in a main array. Each time these numbers change, the words indexed also change, and I already know how they are to be placed in the tree. How do I label the tree with these strings then ?
I guess, the general question is how to label a tree with a bunch of strings?
A bit of a hack is to look at plotted points and, assuming they have a 1-1 correspondence with the nodes in your vector, use their coordinates to plot text.
treeplot([0 1 1]); % plot your tree
c = get(gca, 'Children'); % get handles to children
% grab X and Y coords from the second child (the first one is axes)
x = get(c(2), 'XData');
y = get(c(2), 'YData');
Now you can plot whatever at these coordinates. If labels is a cell array of labels, then you can display them next to the nodes as follows:
text(x, y, labels, 'VerticalAlignment','bottom', ...
'HorizontalAlignment','right')