Using nargin for a menu function
Show older comments
I have a menu function with 4 choices. The first two are buttons to be pressed to enter a start date (choice 1) and an end date (choice 2). Choice 3 cancels the script (it is in a while loop), and choice 4 continues with the dates selected to plot some functions. I want to add code that says "if a start date wasn't chosen, Start_Date1 = datetime('now') - 14" and "if an end date wasn't chosen then End_Date1 = datetime('now') + 1". I tried to do this with the following code for choice 1 of the menu. However, it doesn't work when I run the program and don't select a start or end date manually. When I run it in debug mode, by executing each line at a time in the command window it works... Does anybody know what the issue is? (Note: the code is the same for choice 2 on the menu, except with End_Date1.)
if nargin < 1
Start_Date1 = datetime('now') - 14;
Start_Date1 = dateshift(Start_Date1,'start','day');
day1 = datenum(Start_Date1);
end
4 Comments
Geoff Hayes
on 24 Jul 2017
Kyle - please provide some context for the above code. What is the function for it? A callback? Or is this just in your while loop as
while true
% do stuff
% read menu choice
% etc.
if nargin < 1
% etc.
end
end
What function does the nargin correspond to?
Walter Roberson
on 24 Jul 2017
There are different ways to implement that kind of menu. Please show your code
Kyle Reagan
on 24 Jul 2017
Edited: Kyle Reagan
on 24 Jul 2017
nargin is for arguments. Your code doesn't appear to be using a function where arguments are passed in.
I assume uigetdate returns [] or 0 or something like that if it is closed without a date selection which I assume is what you mean by no date selected. So just use that in a test.
The location of your code is confusing though because you have 4 lines of code that assume a date was chosen before you then try to handle the case of a date not being chosen, unless I am reading your code incorrectly.
Accepted Answer
More Answers (0)
Categories
Find more on Programming 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!