plotGauge(measureme​nt, rangeGrid, numGrid)

Visualize/plot dial / gauge / meter rendered within MATLAB using OpenGL
291 Downloads
Aktualisiert 1. Jun 2016

Lizenz anzeigen

function plotGauge(measurement, rangeGrid, numGrid)
% Plot dial / gauge / meter for depicting a measurement
%
% Input Arguments
% 1) measurement - Measurement to be indicated on the meter/gauage/dial
% 2) rangeGrid - Measurement range; default minimum is 0, enter max value
% only
% 3) varargin - (optional) numGrid: Number of equidistant grids to display
%
% Output
% Dial / Gauge / Meter with measurement indicated with an arrow
%
%
% Example:
% m = 150; % measurement entered by user
% rangeGrid = 480; % default min value = 0. Max value is 480
% numGrid = 6; % Ease of readability selected by user
% testplotGauge(m,rangeGrid,numGrid) % Plot the dial with reading
% th = title('Gauge Reading'); % Set title
% set(th,'FontSize',15, 'Position', [0 1.9 0]) % Set title-properties
%
%
% Developed by
% Sowmya Aggarwal
% email: sowmyaaggarwal@gmail.com
% M.Sc. Carnegie Mellon University
% M.Sc. University of Pittsburgh
% Error handling for the input arguments
if nargin>1

try
m = measurement;
catch
error('Enter measurement value to be displayed on the dial')
end

if ~isnumeric(m)
error('Enter a valid numeric value to be displayed on the dial')

end

try
maxGrid = rangeGrid; %480;
catch
error('Enter the maximum value allowable for the grid-display')
end

if ~isnumeric(maxGrid) || max(size(maxGrid))>1
error('Enter single numeric value for maximum-allowable value on grid display')
end

% exist(numGrid)

else

error('At least two arguments are required. Type: Help plotGauge for details')
end

%% Set coordinates of dial's graphics
% m = measurement;
% maxGrad = rangeGrid;
rangeTheta = 0:0.1:pi;
rO1 = 1.6; % r = radius, O = outer, 1 = first outer? Outer-most if you will
semicrcO1 = rO1.*[cos(rangeTheta); sin(rangeTheta)]; % Outer-most semicircle coordinates
rO = 1.5; % set dimensions of outer radius
semicrcO = rO.*[cos(rangeTheta); sin(rangeTheta)];
rI = 1.2; % set dimensions of inner radius
semicrcI = rI.*[cos(rangeTheta); sin(rangeTheta)];

%% Plot the Dial
figure1 = figure;
axes1 = axes('Parent',figure1);
hold(axes1,'on');
area(semicrcO1(1,:),semicrcO1(2,:),'Facecolor','w')
hold on
area(semicrcO(1,:), semicrcO(2,:), 'FaceColor','b')
hold on
area(semicrcI(1,:), semicrcI(2,:), 'FaceColor','w')
hold on

%% Plot Meter-Reading Grid Lines on the Dial
hold on
plotPolarGrid(maxGrid,numGrid,rO1,rO,'k')

%% Plot meter-reading / dial-reading / gauge-reading for user-input measurement
maxDeg = pi;
theta = (maxDeg/maxGrid)*m;
xStart = 0; %rI*rangeTheta(length(rangeTheta)/2);
r = (rO+rI)/2;

if m<round(maxGrid/2)
deltaX = xStart-rI*abs(cos(-theta));
end

if m==maxGrid/2
deltaX = xStart;
end

if m>maxGrid/2

deltaX = xStart+rI*abs(cos(theta));
end

yStart = 0; %rI*sin(theta(1));
deltaY = r*abs(sin(theta));

% line([xStart deltaX], [yStart deltaY],...
% 'Color','r',...
% 'LineWidth',4);
hold on
% % Line works but adding an arrow head could be a pain
% line([xstart deltaX], [ystart deltaY],'Color','r')

% % annotation works but setting correct position vector is a pain
% annotation(figure1,'arrow',[0.5 0.7], [0 0.5],...
% 'Color',[0.93 0.7 0.1255],'LineWidth',5,'HeadStyle','plain');
quiver(xStart,yStart,deltaX, deltaY,...
'LineWidth',5,...
'MaxHeadSize',0.3,...
'color',[1 0 0])
scatter(xStart,yStart, 'filled','k' )

axis equal

axis off

hold off

end

function plotPolarGrid(maxGrid, numGrid,radOuter,radMedium,varargin)
%% Plot Grid Lines on the Dial
% disp(varargin)

if nargin>3
color_space = varargin{1,1};
color = char(color_space(1));

end

theta(1:numGrid+1) = fliplr((pi/180)*linspace(0,180,numGrid+1));
n = numGrid+1;

for i=1:n

xStart = radMedium*cos(theta(i));
yStart = radMedium*sin(theta(i));

deltaX = radOuter*cos(theta(i));
deltaY = radOuter*sin(theta(i));

line([xStart,deltaX], [yStart,deltaY], 'Color', char(color) );

if i<n/2
text(1.7*cos(theta(i)),1.7*sin(theta(i)),...
num2str((i-1)*(maxGrid/numGrid)),'HorizontalAlignment','right',...
'FontWeight','bold')
else
text(1.7*cos(theta(i)),1.7*sin(theta(i)),...
num2str((i-1)*(maxGrid/numGrid)),'HorizontalAlignment','left',...
'FontWeight','bold')

end




end

end

Zitieren als

Sowmya Aggarwal (2024). plotGauge(measurement, rangeGrid, numGrid) (https://www.mathworks.com/matlabcentral/fileexchange/57470-plotgauge-measurement-rangegrid-numgrid), MATLAB Central File Exchange. Abgerufen.

Kompatibilität der MATLAB-Version
Erstellt mit R2016a
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS Linux
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Veröffentlicht Versionshinweise
1.0

Corrected Output Grids
Added Description