Is there a way to display ALL y points for each x point ?
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lee Lewis
el 21 de Jun. de 2022
Comentada: Voss
el 30 de Jun. de 2022
%load CSV Data for any EMIT data set
%Written by: Lee C Lewis III
%Started: 15 Jun 22
%updated: 21 Jun 22
%ISSUES: As of now the data that has no blank cells in between cells with data the ploting tool works fine. If however
%there are cells without any data adjacent to cells containing data then
%that set of data will not be genereated.
%NOTE: The user may want to clear the last line of data in excel as this data point
%could be so far away from the second to last data point that it skews the
%plot beyond recognition.
%clear
clear;
%recieve file
%have user select a file with pop up window
%Can only grab CSV files
[file,path,indx] = uigetfile(".csv", 'Select a file');
%modify with opts to get rid of NaNs
opts = detectImportOptions(file);
opts = setvartype(opts, 'char');
%read the table
data = readtable(file,opts);
%get the number of rows and columns
numRows = height(data);
numCols = width(data);
%extracting Header titles for describing the frequency that the antennas are
%Transmitting and recieving at.
titleTx = data{1,1};
titleTxValue=data{1,2};
titleHz=data{1,3};
titleRx=data{2,1};
titleRxValue=data{2,2};
titleHz2=data{2,3};
%Turn 1x1 cell arrays into strings in order to make title
titleTxString = char(titleTx);
titleTxValueString = char(titleTxValue);
titleHzString = char(titleHz);
titleRxString = char(titleRx);
titleRxValueString = char(titleRxValue);
titleHz2String = char(titleHz2);
%Combine title values in order to put in one line on graph
titleCombo = {titleTxString ,titleTxValueString,titleHzString, titleRxString, titleRxValueString, titleHz2String};
str = strjoin(titleCombo);
%generate figure
f1 = figure(1);
cla; hold on; grid on;
xAxisTitleFreq = data{3,1};
xAxisDataFreq = cellfun(@str2num,data{4:end,1});
%for loop for generating data under headers i.e(frequency, EMI, etc.)
for i=2:numCols
%Extract data from readData
yAxisDataPwr = data{4:end,i};
%have plot ignore blank cells (Potentially not needed)
%yAxisDataPwr{~cellfun(@isempty, yAxisDataPwr)};
%converts yaxisDataPwr string to a double
yAxisDataPwr = str2double(data{4:end,i});
%grab data for legend
legendHeaderNames = char(data{3,i});
%Plot data
plot(xAxisDataFreq, yAxisDataPwr,'DisplayName', legendHeaderNames);
end
%data Cursor mode, this allows the data point closest to the cursor to be
%displayed after the user clicks the left mouse button.
dcm = datacursormode;
dcm.Enable = 'on';
dcm.DisplayStyle = 'window';
%labeling x&y axis and title
% y-axis is is db but some data points are in dBM, may need to go back and
% try and modify IDK tho.
title(str);
xlabel(xAxisTitleFreq);
ylabel("dB");
lgd=legend;
legend show;
%modifies the number of columns in the legend
lgd.NumColumns = 3;
%Testing purposes: trying to get x and y cordinates. CAN BE COMMENTED OUT
fig2 = uifigure;
holder = [0,0];
tab = uitable(fig2,'Data',holder,'ColumnName',{'X Coord','Y Coord'});
set (gcf, 'WindowButtonMotionFcn', {@mouseMove,tab});
function mouseMove (src,event,tab)
C = get (gca, 'CurrentPoint');
%title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);
MousePt = [C(1,1),C(1,2)];
tab.Data = MousePt;
end
Above is the code I have written, attached is the data I am plotting. Associated with each x cordinate are multiple y cordinates. I have been able to display an individual x and y cordinate. However, I would like to display ALL of the y points for each x point. I have placed an image of what I am trying to recreate below Thank you in advance for the help, I've been scratching my head on this one for awhile.
2 comentarios
Eric Sofen
el 21 de Jun. de 2022
I'm not sure how to get multiple data tips for one x-value. One possible workaround is that the stackedplot chart type has a custom data tip that does show y-values for all lines for a given x-value.
Respuesta aceptada
Voss
el 28 de Jun. de 2022
Editada: Voss
el 28 de Jun. de 2022
Here's an approach that might work ok. The idea is to use the figure's datacursormode with a custom UpdateFcn that sets the datatip insvisible and instead updates other lines and text objects, so that the "datatip" you see is fully customizable.
%load CSV Data for any EMIT data set
%Written by: Lee C Lewis III
%Started: 15 Jun 22
%updated: 21 Jun 22
%updated: 27 Jun 22 (multi-line datatip added by Voss)
%ISSUES: As of now the data that has no blank cells in between cells with data the ploting tool works fine. If however
%there are cells without any data adjacent to cells containing data then
%that set of data will not be genereated.
%NOTE: The user may want to clear the last line of data in excel as this data point
%could be so far away from the second to last data point that it skews the
%plot beyond recognition.
%clear
clear;
%recieve file
%have user select a file with pop up window
%Can only grab CSV files
% [file,path,indx] = uigetfile('.csv', 'Select a file');
[file,path_name] = uigetfile('.csv', 'Select a file');
file = fullfile(path_name,file);
%modify with opts to get rid of NaNs
opts = detectImportOptions(file);
opts = setvartype(opts, 'char');
%read the table
data = readtable(file,opts);
%get the number of rows and columns
numRows = height(data);
numCols = width(data);
%extracting Header titles for describing the frequency that the antennas are
%Transmitting and recieving at.
titleTx = data{1,1};
titleTxValue=data{1,2};
titleHz=data{1,3};
titleRx=data{2,1};
titleRxValue=data{2,2};
titleHz2=data{2,3};
%Turn 1x1 cell arrays into strings in order to make title
titleTxString = char(titleTx);
titleTxValueString = char(titleTxValue);
titleHzString = char(titleHz);
titleRxString = char(titleRx);
titleRxValueString = char(titleRxValue);
titleHz2String = char(titleHz2);
%Combine title values in order to put in one line on graph
titleCombo = {titleTxString ,titleTxValueString,titleHzString, titleRxString, titleRxValueString, titleHz2String};
str = strjoin(titleCombo);
%generate figure
f1 = figure;%(1);
cla; hold on; grid on;
xAxisTitleFreq = data{3,1};
% xAxisDataFreq = cellfun(@str2num,data{4:end,1});
xAxisDataFreq = str2double(data{4:end,1});
lines = [];
%for loop for generating data under headers i.e(frequency, EMI, etc.)
for i=2:numCols
% %Extract data from readData
% yAxisDataPwr = data{4:end,i};
%have plot ignore blank cells (Potentially not needed)
%yAxisDataPwr{~cellfun(@isempty, yAxisDataPwr)};
%converts yaxisDataPwr string to a double
yAxisDataPwr = str2double(data{4:end,i});
%grab data for legend
legendHeaderNames = char(data{3,i});
%Plot data
lines(end+1) = plot(xAxisDataFreq, yAxisDataPwr,'DisplayName', legendHeaderNames);
end
%data Cursor mode, this allows the data point closest to the cursor to be
%displayed after the user clicks the left mouse button.
dcm = datacursormode;
dcm.Enable = 'on';
% dcm.DisplayStyle = 'window';
%labeling x&y axis and title
% y-axis is is db but some data points are in dBM, may need to go back and
% try and modify IDK tho.
title(str);
xlabel(xAxisTitleFreq);
ylabel('dB');
lgd=legend();
legend show;
%modifies the number of columns in the legend
lgd.NumColumns = 3;
N = numel(lines);
data_ax = gca();
% data cursor lines
dcl = line( ...
'Parent',data_ax, ...
'XData',[], ...
'YData',[], ...
'LineStyle','--', ...
'LineWidth',1.5, ...
'Color','k', ...
'HandleVisibility','off', ...
'HitTest','off', ...
'Visible','off');
for ii = 1:N
dcl(ii+1) = line( ...
'Parent',data_ax, ...
'LineStyle','none', ...
'Marker','s', ...
'MarkerFaceColor','k', ...
'MarkerSize',6, ...
'HandleVisibility','off', ...
'HitTest','off', ...
'Visible','off');
end
% datatip axes
ax = axes(gcf(), ...
'Units','pixels', ...
'Position',[0 0 1 1], ...
'Box','on', ...
'XTick',[], ...
'YTick',[], ...
'XLim',[0 1], ...
'YLim',[0 N], ...
'YDir','reverse', ...
'Clipping','on', ...
'ClippingStyle','rectangle', ...
'Visible','off');
% datatip lines
dtl = copyobj(lines,ax);
set(dtl, ...
'HandleVisibility','off', ...
'HitTest','off', ...
'LineStyle','none', ...
'Marker','o', ...
'Visible','off');
for ii = 1:N
set(dtl(ii), ...
'XData',0.1, ...
'YData',ii-0.5, ...
'MarkerFaceColor',get(lines(ii),'Color'));
end
% datatip text
dtt = text(0.2*ones(1,N),(1:N)-0.5,'', ...
'VerticalAlignment','middle', ...
'HorizontalAlignment','left', ...
'Clipping','on', ...
'FontName','FixedWidth', ...
'Visible','off');
dcm.UpdateFcn = {@update_datatip,data_ax,ax,lines,dtl,dtt,dcl};
function txt = update_datatip(src,evt,dax,ax,dl,dtl,dtt,dcl)
set(src,'Visible','off');
txt = '';
N = numel(dtl);
xl = get(dax,'XLim');
yl = get(dax,'YLim');
pos = getpixelposition(dax);
set(ax,'Position',[ ...
pos(1)+10+(evt.Position(1)-xl(1))/(xl(2)-xl(1))*pos(3) ...
pos(2)+10 90 16*N]);
xd = get(dl(1),'XData');
[~,idx] = min(abs(xd-evt.Position(1)));
yd = get(dl,'YData');
if iscell(yd)
yd = vertcat(yd{:});
end
set(dcl(1),'XData',evt.Position([1 1]),'YData',yl);
for ii = 1:N
set(dtt(ii),'String',sprintf('%-.2f',yd(ii,idx)));
set(dcl(ii+1),'XData',xd(idx),'YData',yd(ii,idx));
end
set([dtt(:); dcl(:); dtl(:); ax],'Visible','on');
end
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Title en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!