Clear Filters
Clear Filters

How can one, out of several xlines in a script be ignored when the x-input value is missing?

2 views (last 30 days)
I have several datasets stored as different workspaces. I need to plot these data and mark specific timepoints of blood sampling with an xline. Since there are many plots to be done, I have tried to automate this by using a script in which I only have to note the time that is to be highlighted.
My problem is, for different plots different data samplings is missing. For example:
x = linspace(0,100,10)
y = randi([0 10],1,10)
plot(x,y)
a = input('1:')
b = input('2:')
c = input('3:')
xline(a,'-k')
xline(b,'-r')
xline(c,'-b')
If one of the three timepoints ("1-3") is missing, I want to skip that entry by pressing 'Enter'. How can I get the script not to execute/draw the xline with the missing x-value (or empty array [ ] that is created with the blank input)?
Thank you in advance!

Accepted Answer

Adam Danz
Adam Danz on 3 Jul 2019
Edited: Adam Danz on 3 Jul 2019
You can plot all lines at the same time, skipping empty values. Then you can set the colors of the line.
a = input('1: ');
b = input('2: ');
c = input('3: ');
xlh = arrayfun(@(x)xline(x,'-'),[a,b,c]);
colors = {'k','r','b'};
set(xlh, {'color'}, colors(~cellfun(@isempty,{a,b,c}))')
*This solution was updated after realizing that xline() does not accept NaN values.
  4 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!