Plot electric field in plane
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
0 commentaires
Réponse acceptée
Star Strider
le 26 Juil 2022
Try this —
M1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1077575/data.txt')
[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 commentaires
Star Strider
le 26 Juil 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')
x = M1(:,1);
y = M1(:,2);
z = M1(:,3);
[U1,ix1] = unique(x);
[U2,ix2] = unique(y);
[U3,ix3] = unique(z);
n = numel(U1)
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.
.
Plus de réponses (1)
Abderrahim. B
le 26 Juil 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 commentaire
Voir également
Catégories
En savoir plus sur Contour Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!