How to plot based on the shortest vector?
Show older comments
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 Comments
Rik
on 17 Jun 2021
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
on 18 Jun 2021
Answers (2)
Scott MacKenzie
on 18 Jun 2021
Edited: Scott MacKenzie
on 19 Jun 2021
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 Comments
Rik
on 18 Jun 2021
You should use numel or size. I have not yet seen any situation in which length is a better choice.
Scott MacKenzie
on 18 Jun 2021
@Rik Noted. This just a matter of execution time, right? The difference must be miniscule for vectors, or am I missing something?
Rik
on 19 Jun 2021
I have not tried measuring the execution time, but the differences should be minimal or non-existent.
The broader point is expectations.
If the input array is a vector, there is no difference in output between length and numel. But what kind of errors will you get you suddenly get an array input? If you use it to generate loop indices, you will suddenly only process part of the array. You might get unexpected behavior much later in your code.
If you explicitly tell Matlab what to do, there is no opportunity for confusion. You either want the number of elements, or you want the size along a specific dimension. There will never be a bug or unexpected result.
Why would you want max(size(A)) if you can actually specify what you mean? You say the result is the same for vector inputs, but your code doesn't test if the input actually is a vector.
Rik
on 19 Jun 2021
I got a notification you edited your answer, but since you didn't replace length with numel, I could really find a material edit or a response to my comments.
Scott MacKenzie
on 19 Jun 2021
Edited: Scott MacKenzie
on 19 Jun 2021
@Rik I can't remember but I probably just edited the code comments or fixed some typo in my text. You're right in that I did not at that time change length to numel. I just made the change.
I'm not convinced this is a major issue. Personnaly, I tend to use length with vectors and don't see any imperative in using numel. But, I get your point that people may sometimes have incorrect expectations.
Rik
on 19 Jun 2021
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
on 19 Jun 2021
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.
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))
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.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!