I am trying to plot the magnetic field on a rectangular surface (having x and y coordinates). Hence i have a total of 3 values x,y and B (magnetic field). I have tried plotting the surface plot using normal and transposed datasheet in excel but both of them give me the same data dimensions error. Please help me withi this. I am using the following code till now:
dataset=xlsread('import.xlsx');
x = dataset(:,1);
y = dataset(1,:)
Z = dataset(2:end,2:end)
surf(x,y,Z)
Please see the attached image for clarification.
thank you

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 25 Mai 2020

0 Stimmen

dataset=xlsread('import.xlsx');
x = dataset(2:end,1);
y = dataset(1,2:end);
Z = dataset(2:end,2:end);
surf(x, y, Z.', 'edgecolor', 'none')

3 Kommentare

Azeem Singh Kahlon
Azeem Singh Kahlon am 25 Mai 2020
Thank you so much and I probably understand the logic now that the value on the top left hand corner of the excel sheet is not included in either X or Y and hence would be inconsistent with the dimensions of Z.
Also, thank you for answering so promptly.
Walter Roberson
Walter Roberson am 25 Mai 2020
Notice also the transpose I do on Z.
Azeem Singh Kahlon
Azeem Singh Kahlon am 25 Mai 2020
yes I see that (Z.). However, I do not understand its significance because "Z." and "Z" give the same results.
Thank you once again

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Hélène Parisot-Dupuis
Hélène Parisot-Dupuis am 28 Nov. 2023
Bearbeitet: Walter Roberson am 28 Nov. 2023

0 Stimmen

Hello,
I have the kind of problem with my code and I don't understand why:
for it=1:2
xt(it,1)=it
for jt=1:3
yt(1,jt)=jt
zt(it,jt)=it+(jt-1)
end
end
xt = 1
yt = 1
zt = 1
yt = 1×2
1 2
zt = 1×2
1 2
yt = 1×3
1 2 3
zt = 1×3
1 2 3
xt = 2×1
1 2
yt = 1×3
1 2 3
zt = 2×3
1 2 3 2 0 0
yt = 1×3
1 2 3
zt = 2×3
1 2 3 2 3 0
yt = 1×3
1 2 3
zt = 2×3
1 2 3 2 3 4
figure;
surf(xt,yt,zt,'EdgeColor', 'None', 'facecolor', 'interp');
Error using surf
Data dimensions must agree.
view(2);
colormap(jet(256));
c = colorbar;
Could you help me to find my error please?
Thanks in advance!

3 Kommentare

Remember that rows of an array correspond go y and columns correspond to x . When your x and y are vectors, your z array needs to be length(y) by length(x)
for it=1:2
xt(it,1)=it
for jt=1:3
yt(1,jt)=jt
zt(it,jt)=it+(jt-1)
end
end
xt = 1
yt = 1
zt = 1
yt = 1×2
1 2
zt = 1×2
1 2
yt = 1×3
1 2 3
zt = 1×3
1 2 3
xt = 2×1
1 2
yt = 1×3
1 2 3
zt = 2×3
1 2 3 2 0 0
yt = 1×3
1 2 3
zt = 2×3
1 2 3 2 3 0
yt = 1×3
1 2 3
zt = 2×3
1 2 3 2 3 4
figure;
surf(xt,yt,zt.','EdgeColor', 'None', 'facecolor', 'interp');
view(2);
colormap(jet(256));
c = colorbar;
Walter Roberson
Walter Roberson am 28 Nov. 2023
There are two competing standards for array notation.
  1. When using x and y coordinates, western mathematics tends to put the x first (as rows) and then the y (as columns) -- so in terms of standard Cartesian coordinates, horizontal distance first then vertical distance
  2. When using tables of data, such as a table of costs or a table of trigonmetric values, western mathematics tends to refer to row first and then column -- for example you would look on the row for 1983 and then the column for month -- so vertical distance listed first and then horizontal distance
Any programming language that choses one particular representation (rows mean horizontal distance, rows mean vertical distance) will fail on the other one. So the problem is not a "bug" in MATLAB, or a "mis-design": the problem is competing conventions that cannot both be satisfied.
Thank you for your answer and these clarifications!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by