Plot electric field in plane

8 views (last 30 days)
dj1du
dj1du on 26 Jul 2022
Commented: Star Strider on 26 Jul 2022
Good afternoon,
the png file shows the magnitude of an electric field in a plane, which was calculated by means of an electromagnetic field solver. I exported the field data to a txt file (see attachement), as I want to plot these data in Matlab. How can I create a plot, whose result looks similar to the attached png file?
Thank you very much for your help!

Accepted Answer

Star Strider
Star Strider on 26 Jul 2022
Try this —
M1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1077575/data.txt')
M1 = 968×9
-0.4621 -0.4938 1.2000 0.0020 71.6925 0.2849 67.5267 0.1759 8.3768 -0.3229 -0.4938 1.2000 0.0018 78.7021 0.2855 86.5620 0.1761 4.5099 -0.1836 -0.4938 1.2000 0.0016 82.5231 0.2859 109.7290 0.1763 -0.8245 -0.0444 -0.4938 1.2000 0.0014 80.7989 0.2864 134.9311 0.1765 -6.9604 0.0949 -0.4938 1.2000 0.0012 72.7527 0.2867 158.2912 0.1766 -12.6729 0.2341 -0.4938 1.2000 0.0010 60.3520 0.2870 176.4623 0.1768 -16.8440 0.3734 -0.4938 1.2000 0.0008 46.8712 0.2873 188.6313 0.1769 -19.2588 0.5126 -0.4938 1.2000 0.0007 34.4065 0.2875 196.0995 0.1770 -20.4361 0.6519 -0.4938 1.2000 0.0005 23.3162 0.2877 200.5603 0.1770 -20.9743 0.7911 -0.4938 1.2000 0.0003 13.5545 0.2878 203.0044 0.1771 -21.1254
[U1,ix1] = unique(M1(:,1));
[U2,ix2] = unique(M1(:,2));
% [U3,ix3] = unique(M1(:,3))
n = numel(U1);
m = numel(U2);
nr = reshape(M1(:,1),n,[]);
mr = reshape(M1(:,2),n,[]).';
vr = reshape(M1(:,7),n, []).';
figure
contourf(vr, 100, 'EdgeColor','none')
colormap(turbo)
figure
surfc(vr)
shading('interp')
colormap(turbo)
figure
surfc(vr)
shading('interp')
colormap(turbo)
view(0,90)
It does not look exactly like the posted image, however not enough information has been provided to allow anything else. I first tried readtable, however the variables were not named, so I have no idea what variables are to be used or plotted. I just experimented here until I found a way to re-define the vectors as matrices, and then indexed through the columns until I found something that seemed to resemble the posted image.
Obviously, more information would be helpful!
.
  2 Comments
Star Strider
Star Strider on 26 Jul 2022
O.K., now that I know that information, the result is straightforward —
M1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1077575/data.txt')
M1 = 968×9
-0.4621 -0.4938 1.2000 0.0020 71.6925 0.2849 67.5267 0.1759 8.3768 -0.3229 -0.4938 1.2000 0.0018 78.7021 0.2855 86.5620 0.1761 4.5099 -0.1836 -0.4938 1.2000 0.0016 82.5231 0.2859 109.7290 0.1763 -0.8245 -0.0444 -0.4938 1.2000 0.0014 80.7989 0.2864 134.9311 0.1765 -6.9604 0.0949 -0.4938 1.2000 0.0012 72.7527 0.2867 158.2912 0.1766 -12.6729 0.2341 -0.4938 1.2000 0.0010 60.3520 0.2870 176.4623 0.1768 -16.8440 0.3734 -0.4938 1.2000 0.0008 46.8712 0.2873 188.6313 0.1769 -19.2588 0.5126 -0.4938 1.2000 0.0007 34.4065 0.2875 196.0995 0.1770 -20.4361 0.6519 -0.4938 1.2000 0.0005 23.3162 0.2877 200.5603 0.1770 -20.9743 0.7911 -0.4938 1.2000 0.0003 13.5545 0.2878 203.0044 0.1771 -21.1254
x = M1(:,1);
y = M1(:,2);
z = M1(:,3);
[U1,ix1] = unique(x);
[U2,ix2] = unique(y);
[U3,ix3] = unique(z);
n = numel(U1)
n = 22
xr = reshape(M1(:,1),n,[]);
yr = reshape(M1(:,2),n,[]);
zr = reshape(M1(:,3),n,[]);
E1r = reshape(M1(:,4),n,[]);
E1i = reshape(M1(:,5),n,[]);
E2r = reshape(M1(:,6),n,[]);
E2i = reshape(M1(:,7),n,[]);
E3r = reshape(M1(:,8),n,[]);
E3i = reshape(M1(:,9),n,[]);
Eabs = sqrt((E1r+E1i).^2 + (E2r+E2i).^2 + (E3r+E3i).^2);
figure
surf(xr,yr,zr,Eabs)
colormap(turbo)
shading('interp')
xlabel('X')
ylabel('Y')
view(0,90)
axis('equal')
axis('tight')
figure
surf(xr,yr,zr,Eabs)
colormap(turbo)
shading('interp')
xlabel('X')
ylabel('Y')
% view(0,90)
axis('equal')
axis('tight')
That appears to me to be close to the desired result.
.

Sign in to comment.

More Answers (1)

Issa
Issa on 26 Jul 2022
Hi!
Perhaps this:
clear
close all
Elec_F = readmatrix('data.txt') ;
x = Elec_F(:, 1) ;
y = Elec_F(:, 2) ;
z = Elec_F(:, 3) ;
Ef =abs(sqrt((Elec_F(:,4) + 1i*Elec_F(:,5)).^2 + (Elec_F(:, 6) + 1i*Elec_F(:, 7)).^2 + ...
(Elec_F(:, 8) + 1i*Elec_F(:, 9)).^2) );
scatter3(x,y,z, [] , Ef,"filled", "square", 'MarkerFaceColor','flat')
box on
zlim([0 1.5])
xlim([min(x) max(x)])
ylim([min(y) max(y)])
xticks([])
yticks([])
zticks([])
ax = gca ;
ax.BoxStyle = "full" ;
view([-59.59 22.36])
colorbar
Note that with dense data you will get better resolution.
- Abderrahim
  1 Comment
dj1du
dj1du on 26 Jul 2022
Edited: dj1du on 26 Jul 2022
Thank you very much, this looks really good as a starting point. But isn't there a way to make the plot appear more smooth, e.g. by some interpolation method? Because I don't think denser data can fully resolve the coarse representation.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!