Using nargin for a menu function
显示 更早的评论
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 个评论
Geoff Hayes
2017-7-24
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
2017-7-24
There are different ways to implement that kind of menu. Please show your code
Kyle Reagan
2017-7-24
编辑:Kyle Reagan
2017-7-24
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!