change a vector field into contour lines

2 views (last 30 days)
PAUL
PAUL on 19 Jun 2023
Commented: Cris LaPierre on 19 Jun 2023
I have a program that graphs magntic field as vectors and I'd like to change the vector field to contour lines.
% Read the data from the CSV files
csvFile = 'C:\Users\paull\Desktop\magnetic probe data\coeff.csv';
data = readmatrix(csvFile);
csvFile2 = 'C:\Users\paull\Desktop\magnetic probe data\coeff.angle1.csv';
data2 = readmatrix(csvFile2);
textdata = readtable(csvFile, 'PreserveVariableNames', true);
probe = data(:, 2);
probNum = data(:, 5);
direction = table2cell(textdata(:, 8)); % Convert to cell array
zpos = data(:, 9);
rpos = data(:, 10);
pol = data(:, 13); % Polarity
ok = data(:, 14);
channel = data(:, 6);
NSRC = data(:, 12);
RC = 1 ./ data(:, 4);
angRaw = data2(2, :);
ang = angRaw(1:end-1);
angle = size(channel);
% finds the angle that each of the 10 channels have
for i = 1:192
strValue = num2str(channel(i));
if strcmp(strValue, 'NaN')
angle(i) = 0;
channel(i) = 0;
else
angle(i) = ang(channel(i));
end
if strcmp(num2str(rpos(i)), 'NaN')
rpos(i) = 0;
end
if strcmp(num2str(zpos(i)), 'NaN')
zpos(i) = 0;
end
end
% Create a containers.Map to store the parameter values for each probe
DataMap = containers.Map('KeyType', 'double', 'ValueType', 'any');
% Iterate over the rows and populate the DataMap
for i = 1:size(data, 1)
probeID = probe(i); % Get the probe ID for the current row
% Store the parameter values for the probe
parameters = struct('probe', probe(i), 'direction', direction{i}, ...
'zpos', zpos(i), 'rpos', rpos(i), 'pol', pol(i), 'ok', ok(i), ...
'channel', channel(i), 'NSRC', NSRC(i), 'angle', angle(i), 'RC', RC(i));
if ~isKey(DataMap, probeID)
DataMap(probeID) = parameters;
else
% If the probe already exists in the map, append the new parameters to the existing ones
existingParams = DataMap(probeID);
updatedParams = catstruct(existingParams, parameters);
DataMap(probeID) = updatedParams;
end
end
%organizes the raw data
BzMap = containers.Map('KeyType', 'double', 'ValueType', 'any');
% Iterate over each probe and store its Bz measurements in the hashtable
for i = 1:192
probeID = i;
BzMap(probeID) = rawdata(i, :);
end
% Accessing Bz measurement for probe 10 at time step 480
probeID = 10;
timeStep = 492;
BzValue = rawdata(timeStep, probeID);
% Iterate over each probe and store its Bz measurements in the hashtable
for i = 1:192
probeID = i;
BzMap(probeID) = rawdata(i, :);
end
% Manually set the axis limits
xlim([0 0.3]); % Set the x-axis limits from 2 to 8
ylim([0 0.3]); % Set the y-axis limits from -1 to 1
mag = zeros(size(probNum));
hold on;
zm = size(rpos);
rm = size(rpos);
% Iterate over each probe and plot the corresponding unit vector
for i = 1:numel(probe)
probeID = probe(i);
params = DataMap(probeID);
directionValue = params.direction;
polarity = params.pol; % Get the polarity of the probe
% Check the direction value
if strcmp(directionValue, 'z')
% Plot a unit vector parallel to the z direction
mag(i) = rawdata(timeStep, i) * NSRC(i);
rm(i) = 0;
zm(i) = 1 * mag(i);
if abs(mag(i)) > 1
mag(i) = 0;
end
if polarity < 0
quiver3(zpos(i), rpos(i), 1, -1, 0, 0, 'LineWidth', 2, 'Color', 'b', 'AutoScaleFactor', (mag(i) * 2));
else
quiver3(zpos(i), rpos(i), 1, 1, 0, 0, 'LineWidth', 2, 'Color', 'b', 'AutoScaleFactor', (mag(i) * 2));
end
elseif strcmp(directionValue, 't')
% Calculate the angle offset from the r direction
angleValue = params.angle;
x = sin(pi - angleValue);
y = cos(pi - angleValue);
z = 0;
mag(i) = rawdata(timeStep, i) * NSRC(i) * pol(i) * -1;
if abs(mag(i)) > 1
mag(i) = 0;
end
rm(i) = -x * mag(i);
zm(i) = y * mag(i);
% Plot a unit vector at the specified angle offset from the r direction
quiver3(zpos(i), rpos(i), 1, x, y, z, 'LineWidth', 2, 'Color', 'r', 'AutoScaleFactor', (mag(i) * 2));
else
% Plot a dot for probes with NaN direction values
scatter3(zpos(i), rpos(i), 1, 'Marker', '.', 'MarkerFaceColor', 'k');
end
end
% Adjust the aspect ratio of the plot
pbaspect([5 5 5]);
% Adjust the viewing angle
view(6,30);
  1 Comment
Cris LaPierre
Cris LaPierre on 19 Jun 2023
Please attach your data files to your post using the paperclip icon.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!