How to plot based on the shortest vector?

조회 수: 3 (최근 30일)
Joseph Tirado
Joseph Tirado 2021년 6월 17일
댓글: Scott MacKenzie 2021년 6월 19일
I want to plot two collumns of data against each other but one is longer than the other. The data all lines up and there is only a couple of extra data points at the bottom of the collumn.
Here is the code in question:
patch(po,pa,x,'Facecolor',[0.8,0.8,0.8]);
hold on
AltvO3=plot(O3ave,adj_ave_alt,'-o','Markersize',3,'color','k');
In this case I want pa and x to include up to as many data points in po and adj_ave_alt to include up to as many data points as O3ave.
  댓글 수: 2
Rik
Rik 2021년 6월 17일
The easiest solution is to write a wrapper function that trims the data as you need and then calls plot or patch with the shortened arrays.
Joseph Tirado
Joseph Tirado 2021년 6월 18일
I think you are overestimating my abilities. How would I do that?

댓글을 달려면 로그인하십시오.

답변 (2개)

Scott MacKenzie
Scott MacKenzie 2021년 6월 18일
편집: Scott MacKenzie 2021년 6월 19일
So, the approach I think you are willing to take is just to ignore the few extra elements that are in the longer of the two vectors. Here's a simple way to do this:
% two column vectors of data with a few extra elements in one of them
c1 = randi([1 100], 20, 1);
c2 = randi([1 100], 18, 1);
% determine the length of the shorter of the two columns of data
n = min(numel(c1), numel(c2));
% plot the data, ignoring the extra elements in the longer vector
patch(c1(1:n), c2(1:n), 'r');
  댓글 수: 7
Rik
Rik 2021년 6월 19일
I just don't see the merit of using something that has the potential to cause issues, while there is a function that does the exact same thing for vectors, but will not cause unintended behavior for array inputs.
I don't see why you would teach anyone a tool that may bite them, while there are viable alternatives. Can you convince me why numel would be a worse choice?
Scott MacKenzie
Scott MacKenzie 2021년 6월 19일
This seems like splitting hairs to me. If you think this is significant, perhaps take it up with MATLAB staff. They could easily add "not recommended" in the documentation, as done with other functions people should avoid.

댓글을 달려면 로그인하십시오.


Rik
Rik 2021년 6월 18일
I was just about to comment something along the lines of the answer @Scott MacKenzie posted. The idea is that you find out what the shortest vector is and then call the function you need. The only extra part I was suggesting was a function definition and some documentation:
h=short_plot(rand(10,1),rand(8,1))
h =
Line with properties: Color: [0 0.4470 0.7410] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [0.9257 0.3860 0.4225 0.3850 0.0851 0.7865 0.0147 0.9023] YData: [0.4146 0.5354 0.7882 0.1400 0.9720 0.7587 0.7900 0.7639] ZData: [1×0 double] Show all properties
function varargout=short_plot(x,y,varargin)
%This functions shortens either x or y to make them equal length and calls
%the plot function. It will forward all optional inputs to plot().
n=min(numel(x),numel(y));
x=x(1:n);y=y(1:n);
varargout=cell(nargout,1);
[varargout{:}]=plot(x,y,varargin{:});
end
You can do the same for your custom patch function.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by