Reputation: 1
In addition to the regular data being plotted, I am trying to plot a bunch of horizontal constant lines, which represent important thresholds in that regular data. The number of constants is variable, comes from an array.
For instance, if there are 3 thresholds at 1, 2 and 3, the array would be thresholds = [1,2,3]. If there were four thresholds at 1, 4, 7 and 10, it would be thresholds = [1,4,7,10].
I use a special plot function (let's call it plot_special), to plot the data. It just takes the x axis in first and the rest of the arguments are data to plot. So in the case of some random data with the four thresholds above it would be:
t = time for the data
data1 = some data already created
data2 = some other data already created
thresholds = [1,4,7,10];
f = ones(size(t));
plot_special(t, data1, data2, f*thresholds(1), f*thresholds(2), f*thresholds(3), f*thresholds(4));
The issue is that thresholds can be variable length, so I pretty much need a way to iterate through thresholds as I did in the above example. Clearly I need to use size(thresholds) but I am not sure how to include it all in the function call.
Upvotes: 0
Views: 31
Reputation: 114518
There is a much simpler way to do this: just pass thresholds
to your function:
plot_special(t, data1, data2, thresholds);
Inside the function, you can plot the thresholds since you already have t
available. You don't need to plot a threshold value for each t
, just for the first and last values. You don't even need a loop to do this since a matrix of y-values will be plotted by column against an x-vector that matches the row dimension (thanks @excaza):
x = [t(1); t(end)];
y = repmat(thresholds(:)', [2, 1]);
plot(x, y)
Upvotes: 1