Inconsistent dimension error when plotting mesh in Matlab - matlab

I'm new to Matlab and I want to plot a mesh. My coordinates are:
x = [30 34 38 40 44 48 50]
y = [1:5:20]
Z = [9.1 8.5 7.83 7.54 7.07 6.61 6.49 ;
14.5 8.96 8.21 7.71 7.07 6.61 6.4;
13.37 13.4 10.2 9.4 9 7.3 7.9;
12.09 12 12.14 11.96 13.58 14.12 14.311;
14.97 10.77 11.87 12.4 13.62 14.19 14.94]
Ehen I tried to plot it in Matlab it gives following error:
Data point coordinates have inconsistent dimension.

You have indeed inconsistent dimensions as you need 5 elements in y. Also you need a matrix Z, not a vector.
The following should get you starting:
y = [0:5:20]
%// reshape z in case z is a vector
z = reshape(Z,numel(y),numel(x))
figure(1)
%// mesh(x,y,z)
surf(x,y,z) % colored mesh
Check the data sizes and adjust the reshaping according to your needs!

In general, if X and Y are vectors, length(X) = n and length(Y) = m, where `[m,n] = size(Z)v.
In your case:
length(x)
7
length(y)
4
but
size(Z)
1 35
So you need to reshape the Z.
Do:
x = [30 34 38 40 44 48 50]
y = [0:5:20]
Z = [9.1 8.5 7.83 7.54 7.07 6.61 6.49 ; 14.5 8.96 8.21 7.71 7.07 6.61 6.4; 13.37 13.4 10.2 9.4 9 7.3 7.9; 12.09 12 12.14 11.96 13.58 14.12 14.311; 14.97 10.77 11.87 12.4 13.62 14.19 14.94]
Z = reshape(Z,numel(y),numel(x))
mesh(x,y,Z)

Related

How to create a contourf plot from a table?

As far as I understand the way to 3-d plot/ surface plot is "meshgrid".
But the data I have has a specific format:
X
Y
Z
1
0.1
10
1
0.2
12
1
0.3
13
2
0.1
11
2
0.2
12
2
0.3
14
3
0.1
11
3
0.2
12
3
0.3
15
The first and second column (X and Y) repeat themselves in that fashion, and I need to plot Z(X,Y).
How Do I do it?
X = 1:1:3 % grid can be set by beginning:step:end
Y = 0.1:0.1:0.3
[x,y] = meshgrid(X,Y); % then make 2d domain
% z values have to be done manually or can automate if you read your data from txt file
z = [10 12 13; 11 12 14; 11 12 15];
% and finally
surf(x,y,z)

How to sample a plot in Matlab?

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

matlab: interpolation vector with a lot of duplicate values

Maybe my question might seem a little strange.
I have 3 vectors x1, y1, and x2
x1 and x2 have a lot of duplicate values, I would like to get y2 by interpolation with the same lenght of y1
y1 = [350 770 800 920 970 990 1020 1054 1080 1100];
x1=[10 10 11 14 13 12 10 10 10 7];
x2 = [10 10 13 13 15 13 13 10 10 10];
(actually have greater length, but always the same for all)
Is it impossible or a non-sense question? (in any case, my problem would remain unsolved)

How I can create a movie from a bunch of plots

I am really a beginner to make a movie. I need to make a movie from a bunch of plots. How I can make it.
I have 8 plots , which are y versus x. I want to make movie based on x to see how y change with increasing x.
Please consider my matlab file is as below:
x=[1 1.2 1.4 2 3 4 5 7 9 10];
y1=[2.8 7.6 10.9 12.3 15.0 21 12.3 14.5 42.4 47.7];
y2=1e8.*[0.599e-7 0.607e-7 0.343e-7 0.3e-7 0.873e-8 0.578e-8 0.298e-8 0.725e-9 0.14e-8 0.478e-9];
y3=10.*[0.136 0.544 0.834 1.03 0.366 0.314 0.703 0.207 0.696 0.164];
y4=10.*[0.26 0.21 0.17 0.25 0.31 0.34 0.16 0.15 0.13 0.31];
y5=....
y6=...
y7=...
y8=[6 7.6 10.9 12.3 15.0 21 12.3 19.5 42.4 47.7 ];
plot(x,y1)
hold on
plot(x,y2)
hold on
plot(x,y3)
hold on
plot(x,y4)
hold on
plot(x,y5)
hold on
plot(x,y6)
hold on
...
plot(x,y8)
I wrote following as a simple example but it does not work:
clc;clear all;
x=[1 1.2 1.4 2 3 4 5 7 9 10];
y = zeros(4,10);
y(1,:)=[2.8 7.6 10.9 12.3 15.0 21 12.3 14.5 42.4 47.7 ];
y(2,:)=1e8.*[0.599e-7 0.607e-7 0.343e-7 0.3e-7 0.873e-8 0.578e-8 0.298e-8 0.725e-9 0.14e-8 0.478e-9];
y(3,:)=10.*[0.136 0.544 0.834 1.03 0.366 0.314 0.703 0.207 0.696 0.164 ];
y(4,:)=10.*[0.26 0.21 0.17 0.25 0.31 0.34 0.16 0.15 0.13 0.31 ];
figure;
hold on
for n = 1:10
plot(x,y)
M(n)=getframe; % get frame from the current figure;
end
movie(M,10); %plays movie M 10 times
In a nut shell , How I can make a movie like Fig. 3.2: in the following link
The basic code for making a movie from a given figure is like this:
figure;
hold on
for n = 1:20
plot(1:10,n*(1:10))
M(n)=getframe; % get frame from the current figure;
end
movie(M,10); %plays movie M 10 times
Look into the help files for getframe and movie to see what other options are available. To save a movie out, see movie2avi.
Note that for a given figure you only need to call hold on once. If all your y values are of the same length, then you can re-write your code/plotting more efficiently like this:
x=[1 1.2 1.4 2 3 4 5 7 9 10];
y = zeros(8,10);
% then this for each of your sets of y values
y(1,:) = [2.8 7.6 10.9 12.3 15.0 21 12.3 14.5 42.4 47.7 53 81.3 86.1];
...
y(8,:)=[6 7.6 10.9 12.3 15.0 21 12.3 19.5 42.4 47.7 53 81.3 86.1 ];
plot(x,y) % plots all the y's against x with automatic coloring
plot(x,y(n,:)) % plots just one set of y values against x

How to use interpolation in MATLAB?

Could anyone help how should I use the interp1-function on MATLAB on data such as the following:
-99
3
1
7
10
10
22
29
-99
-99
25
26
60
142
78
124
74
26
13
18
The -99 data values correspond to error-values and I would like to interpolate them...advices? =) Thank you!
Not around Matlab at the moment but I reckon you could do something like this:
Y = [-99
3
1
7
10
10
22
29
-99
-99
25
26
60
142
78
124
74
26
13
18];
%Make an array of x values - I'm assuming yours are evenly spaced
Xi = 1:length(Y);
%remove the -99 points
errors = Y == -99;
X = Xi(~errors);
Y = Y(~errors);
Yi = interp1(X, Y, Xi);
So in the code X and Y are the x and y coord vectors of the points you want to interpolate, i.e. your input points (without the errors!), and Xi is a vector of the location of the points you would like to interpolate values for (if you think about X being location and Y being value). The point Yi will be the interpolated values corresponding to Xi, which I think in this case will be your original vector with the -99 points replaced by linearly interpolated values. Funny stuff might happen at the edges :/ check the docs. Hope there are no errors.
Lets say your array is a.
x = find(a ~= -99);
y = a(x);
xi = 1:length(a);
yi = interp1(x, y, xi);
yi is what you are looking for.