How to make contour plot of two matrices in MATLAB?

I have two data files (Data_file1 and Data_file2) to do a comparison on.
Data files both are matrices with 1 row and 55 columns, which represent 55 points. The points are very similar in value, So plotting them as points wouldn't give a good figure for demonstrating differences. This is why I want to plot each point as a contour.
Here is the code I generated, it technically should work, but somehow it doesn't. Can anyone spot any error that I can't see?
figure;
sm=smithplot(gca);
hold on
NumPowCon=3;
x = linspace(-1,1,55)';
y = linspace(-1,1,55)';
[X,Y] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y)));
F = TriScatteredInterp(x,y,Data_file1);
Z = F(X,Y);
contour(X,Y,Z,NumPowCon,'r-')
F = TriScatteredInterp(x,y,Data_file2);
Z = F(X,Y);
contour(X,Y,Z,NumPowCon,'b--')

2 Comments

We do not have your data so we cannot test your code to see what "doesn't work" means to you.
I notice you are not using subplot() or tiled layout or figure() or hold on so the second contour might erase the first.
Thanks for the comment. But it stops at the first contour ploting. The error is 'Xdata' and YData' must be equal the number of columns and rows in 'ZData'. I will add the data files to test the code if you can.

Sign in to comment.

 Accepted Answer

Data files both are matrices with 1 row and 55 columns, which represent 55 points.
They are vectors by definition, and it is not possible to plot a contour of a vector.
You could vertically concatenate the two row vectors to create a (2x55) matrix and then take the contour of that. I am not certain how meaningful that would be.
It would be necessary to have the vectors to work with in order to determine if it would be possible to do anything with them. For example, if they are ‘z’ vectors, do they have corresponding ‘x’ and ‘y’ vectors that could indicate that they are actually gridded and so could be reshaped to each create a surface?
.

8 Comments

Thanks. They are some real numbers between -1 and +1. So I was thinking assuming the value as radius in same y and x direction can be used to plot some sort of 2D contours. I have attached data files.
I am lost. I understand that these data are used in a Smith chart (that I have some experience with, although not in MATLAB because I do not have the relevant Toolboxes and so have no experience with those funcitons), however I have no idea what you want to do with them. I doubt that a Smith chart lends itself to any sort of contour plot.
LD1 = load(websave('Data_file1','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1209953/Data_file1.mat'))
LD1 = struct with fields:
Data_file1: [55×1 double]
DataFile1 = LD1.Data_file1
DataFile1 = 55×1
-0.0094 0.3127 0.3084 0.2663 0.1882 0.0817 -0.0323 -0.1204 -0.2144 -0.2783
LD2 = load(websave('Data_file2','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1209958/Data_file2.mat'))
LD2 = struct with fields:
Data_file2: [55×1 double]
DataFile2 = LD2.Data_file2
DataFile2 = 55×1
-0.0094 0.3110 0.3085 0.2680 0.1909 0.0835 -0.0326 -0.1205 -0.2152 -0.2796
figure
subplot(2,1,1)
plot(DataFile1)
grid
subplot(2,1,2)
plot(DataFile2)
grid
a = linspace(0, 2*pi, numel(DataFile1));
figure
subplot(1,2,1)
smithplot(DataFile1)
grid
subplot(1,2,2)
smithplot(DataFile2)
grid
What do you want to do with these vectors, and is the Smith chart at all relevant?
.
lets just have a look to the first two terms of each datafile and look at the screenshot I atached.
I want to use those numbers to plot such a circles.
how long should each red segment be?
Oh, that's just line style I put to make diference between data file1 (blue) and 2(red) to make that image. If you assume the numbers in data files as radius and plot them in one figure they should have overlap as in picture.
I was in the process of writing this when my computer crashed (again). I hate Windows!
I assume that the dash or dash-dot lines simply denote different lines rather than a specific pattern. I have no idea how to interpret it.
Anyway ...
LD1 = load(websave('Data_file1','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1209953/Data_file1.mat'));
DataFile1 = LD1.Data_file1
DataFile1 = 55×1
-0.0094 0.3127 0.3084 0.2663 0.1882 0.0817 -0.0323 -0.1204 -0.2144 -0.2783
LD2 = load(websave('Data_file2','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1209958/Data_file2.mat'));
DataFile2 = LD2.Data_file2
DataFile2 = 55×1
-0.0094 0.3110 0.3085 0.2680 0.1909 0.0835 -0.0326 -0.1205 -0.2152 -0.2796
a = linspace(0, 2*pi, numel(DataFile1)).';
V = ones(size(DataFile1)).';
A = a * V;
R1 = DataFile1 * V;
R2 = DataFile2 * V;
[X1,Y1] = pol2cart(A, R1.');
[X2,Y2] = pol2cart(A, R2.');
figure
plot(X1, Y1)
grid
axis('equal')
title('Data File 1')
figure
plot(X2, Y2)
grid
axis('equal')
title('Data File 2')
figure
h1 = plot(X1, Y1, '-b', 'DisplayName','Data File 1');
hold on
h2 = plot(X2, Y2, '-r', 'DisplayName','Data File 2');
hold off
grid
axis('equal')
legend([h1(1) h2(1)], 'Location','best')
This is as close as I can come to a contour plot for these. The provided vectors are the radii of concentric circles.
Another option would be to use polarplot.
Make appropriate changes to produce the desired results.
.
Oh I hate windows too but not brave enough to shift to Linux. And yes you are right those dash-dot lines were only to indicate differences. You did a great job there, that's exactly what I was trying to do for hours, thank you so much.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!