| 1 | + | classdef DataMapper < matlab.mixin.SetGet |
| 2 | + | % Class for mapping data for a grid |
| 3 | + | |
| 4 | + | properties |
| 5 | + | posStep; % Grid spacing in each direction |
| 6 | + | |
| 7 | + | % Data at position n will be mapped to grid point |
| 8 | + | % (posInd(n,1), posInd(n,2)) |
| 9 | + | posInd; % Index of the |
| 10 | + | |
| 11 | + | % Number of steps in each axis |
| 12 | + | nsteps; |
| 13 | + | |
| 14 | + | % Position along each axis |
| 15 | + | posx, posy; |
| 16 | + | |
| 17 | + | end |
| 18 | + | |
| 19 | + | methods |
| 20 | + | function obj = DataMapper(pos, posStep) |
| 21 | + | % Constructor |
| 22 | + | % |
| 23 | + | % pos is a n x 2 list of all possible positions. |
| 24 | + | % posStep is the 1 x 2 grid spacing in each direction |
| 25 | + | |
| 26 | + | % Compute the indices that each position gets mapped to |
| 27 | + | posMin = min(pos); |
| 28 | + | posMax = max(pos); |
| 29 | + | obj.posStep = posStep; |
| 30 | + | obj.nsteps = (posMax - posMin)./posStep + 1; |
| 31 | + | obj.posInd = round((pos-posMin)./posStep)+1; |
| 32 | + | |
| 33 | + | % Compute the x and y positions for the imagesc function |
| 34 | + | obj.posx = posMin(1) + (0:obj.nsteps(1)-1)*posStep(1); |
| 35 | + | obj.posy = posMin(2) + (0:obj.nsteps(2)-1)*posStep(2); |
| 36 | + | end |
| 37 | + | |
| 38 | + | function plot(obj, data, options) |
| 39 | + | % Plots data with the positions |
| 40 | + | % |
| 41 | + | % Data is a vector of size n x 1. The function will then |
| 42 | + | % plot the value of data(i) in pos(i,1), pos(i,2). |
| 43 | + | % |
| 44 | + | % It can also optionally create dummy legend labels that can |
| 45 | + | % then be displayed with the legend command |
| 46 | + | arguments |
| 47 | + | obj |
| 48 | + | data |
| 49 | + | options.legendValues = [] |
| 50 | + | options.legendLabels = [] |
| 51 | + | options.plotHold logical = false |
| 52 | + | options.addLegend logical = false |
| 53 | + | options.legendLocation string = 'southeast' |
| 54 | + | options.noDataValue = 0 |
| 55 | + | end |
| 56 | + | |
| 57 | + | |
| 58 | + | % Create an image array for the data |
| 59 | + | posImage = repmat(options.noDataValue, ... |
| 60 | + | obj.nsteps(2), obj.nsteps(1)); |
| 61 | + | for i = 1:length(data) |
| 62 | + | posImage(obj.posInd(i,2), obj.posInd(i,1)) = data(i); |
| 63 | + | end |
| 64 | + | |
| 65 | + | % Plot the data |
| 66 | + | imagesc(obj.posx, obj.posy, posImage); |
| 67 | + | hold on; |
| 68 | + | |
| 69 | + | % Plot a dummy rectangle for each color |
| 70 | + | if ~isempty(options.legendLabels) |
| 71 | + | nlabels = length(options.legendLabels); |
| 72 | + | colorArr = parula(nlabels); |
| 73 | + | for i = 1:nlabels |
| 74 | + | fill([1,1],[1,1], colorArr(i,:), 'DisplayName', ... |
| 75 | + | options.legendLabels{i}); |
| 76 | + | end |
| 77 | + | end |
| 78 | + | |
| 79 | + | % Turn off the plot hold if not requested |
| 80 | + | if ~options.plotHold |
| 81 | + | hold off; |
| 82 | + | end |
| 83 | + | |
| 84 | + | % Create the legend |
| 85 | + | if options.addLegend |
| 86 | + | legend('Location', options.legendLocation); |
| 87 | + | end |
| 88 | + | end |
| 89 | + | end |
| 90 | + | end |
| 91 | + | |
| 92 | + | |