How to request additional data from the user in a function

2 次查看(过去 30 天)
Hi, Thanks for reading this.
I have to call a function, then it appears I need to request more data from within the function. Data the user is supposed to type on the screen. How do I do this? The problem is stated as:
I have to admit I'm a little unclear with regard to what the problem statement is requesting.
  11 个评论
Stephen23
Stephen23 2018-5-10
@Lauren Jablonowski: none of your for loops make any sense. Get rid of them. Use one if statement.
Lauren Jablonowski
Lauren Jablonowski 2018-5-18
@Stephen Cobeldick: thanks! with the if statement and isscalar function I was able to fully solve this.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2016-12-8
编辑:Stephen23 2016-12-8
Something like this:
function out = year2016(m)
VN = datenum([2016,m,1]):datenum([2016,m+1,1])-1;
DN = 1+mod(VN-3,7);
MC = {'January';'February';'March';'April';'May';'June';'July';'August';'September';'October';'November';'December'};
DC = {'Mon','Tue','Wed','Thu','Fri','Sat','Sun'};
out = struct('day',DC(DN),'date',num2cell(1:numel(VN)));
[out(:).month] = deal(MC{m});
end
And tested:
>> m = year2016(12); m(8)
ans =
day = Thu
date = 8
month = December
>> m = year2016(1); m(1)
ans =
day = Fri
date = 1
month = January
>> m = year2016(2); m(29)
ans =
day = Mon
date = 29
month = February
  3 个评论
Stephen23
Stephen23 2016-12-8
编辑:Stephen23 2016-12-8
@DJ V: I used datenum and colon notation to generate a vector of all serial date numbers for the selected month. Then mod to determine the day of the week as an index from 1 to 7. The day field is generated using this index to select the day of the week from a cell array of strings. The date field is generated by simply generating a vector of numbers from 1 to the length of the day field. The month field is simply filled in by using the input month to select the month string from a cell array of strings.
Here are the first two critical lines:
>> m = 2; % feb
>> VN = datenum([2016,m,1]):datenum([2016,m+1,1])-1
VN =
736361 736362 736363 736364 736365 736366 736367 736368 736369 736370 736371 736372 736373 736374 736375 736376 736377 736378 736379 736380 736381 736382 736383 736384 736385 736386 736387 736388 736389
>> DN = 1+mod(VN-3,7)
DN =
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1
DN is a vector of all days of the week for the month. Check it yourself if you do not believe me (Hint: the first and last days of February 2016 were both Mondays, which is what my code shows above). This vector DN is simply used as an index to give the days corresponding to the dates, exactly as requested. The dates is a simple vector from 1 to numel(..) because where I come from the first day of the month is the 1st.
"I don't see how this will make the days of the month correspond to the dates"
That is okay. Use MATLAB's documentation to help you understand how the functions I used work. Try them out yourself: play around with them, try different values. When you do this you might learn some much better ways to write code than relying on if's and copy-and-pasting hundreds of lines of text.

请先登录,再进行评论。

更多回答(3 个)

Geoff Hayes
Geoff Hayes 2016-12-7
DJ - the function returns an array of structs where each element corresponds to a day of the month. So if the user passes in an integer that is outside of the interval [1,12], then an empty array is returned. If the integer is within this interval, then the array has that many structs as days in the month. For example, if 1 is the passed in integer, then the output is an array of 31 elements where m(1) corresponds to January 1rst, m(2) corresponds to January 2nd, etc.

Walter Roberson
Walter Roberson 2016-12-7
Your code uses (for example)
for n = 1 :31
datevalue(n) =n;
monthvalue(n)='August';
end
assigns numeric values to datevalue, so datevalue(1) is a numeric scalar.
Then after that you
dayvalue(1)='Mon';
or similar. That attempts to assign the 3-element character vector ['M', 'o', 'n'] to the single numeric scalar position dayvalue(1)
In order to be able to make the assignment
dayvalue(1)='Mon';
you would need to modify your initialization loop to
for n = 1 :31
datevalue(n) = strings(1);
monthvalue(n)='August';
end
(there are other better ways, but this is the minimum change.)
You will need to be using R2016b to do this. If you are using any earlier release then it is not possible to do
dayvalue(1)='Mon';
and you would need to switch to cell arrays such as
dayvalue{1}='Mon';
after having change the initialization to
for n = 1 :31
datevalue(n) = cell(1);
monthvalue(n)='August';
end
(again, there are better ways.)
  6 个评论
DJ V
DJ V 2016-12-8
Okay, what follows works. It isn't graded as right by the software, but it does what its supposed to do. I think the problem is the string declaration instead of using an array of chars for the three letter day of week abbreviations. Any idea how to do that with chars instead of a string?
function S = year2016(i)
%YEAR2016 Summary of this function goes here
% Detailed explanation goes here
cest=true;
for month = 1:12
if i == month
cest = false;
end
if month ==12 && cest == true
S = [];
break;
end
end
if i ==4 || i == 9 ||i == 11 || i ==6 || i == 2
if i ==4
monthval = 'April';
daycount = 30;
for n = 1:30
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Fri';
dayvalue(2)='Sat';
dayvalue(3)='Sun';
dayvalue(4)='Mon';
dayvalue(5)='Tue';
dayvalue(6)='Wed';
dayvalue(7)='Thu';
dayvalue(8)='Fri';
dayvalue(9)='Sat';
dayvalue(10)='Sun';
dayvalue(11)='Mon';
dayvalue(12)='Tue';
dayvalue(13)='Wed';
dayvalue(14)='Thu';
dayvalue(15)= 'Fri';
dayvalue(16)='Sat';
dayvalue(17)='Sun';
dayvalue(18)='Mon';
dayvalue(19)='Tue';
dayvalue(20)='Wed';
dayvalue(21)='Thu';
dayvalue(22)='Fri';
dayvalue(23)='Sat';
dayvalue(24)='Sun';
dayvalue(25)='Mon';
dayvalue(26)='Tue';
dayvalue(27)='Wed';
dayvalue(28)='Thu';
dayvalue(29)='Fri';
dayvalue(30)='Sat';
end
if i == 6
monthval = 'June';
daycount = 30;
for n = 1:30
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Wed';
dayvalue(2)='Thu';
dayvalue(3)='Fri';
dayvalue(4)='Sat';
dayvalue(5)='Sun';
dayvalue(6)='Mon';
dayvalue(7)='Tue';
dayvalue(8)='Wed';
dayvalue(9)='Thu';
dayvalue(10)= 'Fri';
dayvalue(11)='Sat';
dayvalue(12)='Sun';
dayvalue(13)='Mon';
dayvalue(14)='Tue';
dayvalue(15)='Wed';
dayvalue(16)='Thu';
dayvalue(17)='Fri';
dayvalue(18)='Sat';
dayvalue(19)='Sun';
dayvalue(20)='Mon';
dayvalue(21)='Tue';
dayvalue(22)='Wed';
dayvalue(23)='Thu';
dayvalue(24)='Fri';
dayvalue(25)='Sat';
dayvalue(26)='Sun';
dayvalue(27)='Mon';
dayvalue(28)='Tue';
dayvalue(29)='Wed';
dayvalue(30)='Thu';
end
if i == 9
monthval = 'September';
daycount = 30;
for n = 1:30
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Thu';
dayvalue(2)='Fri';
dayvalue(3)='Sat';
dayvalue(4)='Sun';
dayvalue(5)='Mon';
dayvalue(6)='Tue';
dayvalue(7)='Wed';
dayvalue(8)='Thu';
dayvalue(9)= 'Fri';
dayvalue(10)='Sat';
dayvalue(11)='Sun';
dayvalue(12)='Mon';
dayvalue(13)='Tue';
dayvalue(14)='Wed';
dayvalue(15)='Thu';
dayvalue(16)='Fri';
dayvalue(17)='Sat';
dayvalue(18)='Sun';
dayvalue(19)='Mon';
dayvalue(20)='Tue';
dayvalue(21)='Wed';
dayvalue(22)='Thu';
dayvalue(23)='Fri';
dayvalue(24)='Sat';
dayvalue(25)='Sun';
dayvalue(26)='Mon';
dayvalue(27)='Tue';
dayvalue(28)='Wed';
dayvalue(29)='Thu';
dayvalue(30)='Fri';
end
if i == 11
monthval = 'November';
daycount = 30;
for n = 1:30
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Tue';
dayvalue(2)='Wed';
dayvalue(3)='Thu';
dayvalue(4)='Fri';
dayvalue(5)='Sat';
dayvalue(6)='Sun';
dayvalue(7)='Mon';
dayvalue(8)='Tue';
dayvalue(9)='Wed';
dayvalue(10)='Thu';
dayvalue(11)= 'Fri';
dayvalue(12)='Sat';
dayvalue(13)='Sun';
dayvalue(14)='Mon';
dayvalue(15)='Tue';
dayvalue(16)='Wed';
dayvalue(17)='Thu';
dayvalue(18)='Fri';
dayvalue(19)='Sat';
dayvalue(20)='Sun';
dayvalue(21)='Mon';
dayvalue(22)='Tue';
dayvalue(23)='Wed';
dayvalue(24)='Thu';
dayvalue(25)='Fri';
dayvalue(26)='Sat';
dayvalue(27)='Sun';
dayvalue(28)='Mon';
dayvalue(29)='Tue';
dayvalue(30)='Wed';
end
if i ==2
monthval = 'February';
daycount = 29;
dayvalue(1:29)= string(1);
for n = 1:29
datevalue(n) =n;
end
dayvalue(1)='Mon';
dayvalue(2)='Tue';
dayvalue(3)='Wed';
dayvalue(4)='Thu';
dayvalue(5)='Fri';
dayvalue(6)='Sat';
dayvalue(7)='Sun';
dayvalue(8)='Mon';
dayvalue(9)='Tue';
dayvalue(10)='Wed';
dayvalue(11)='Thu';
dayvalue(12)='Fri';
dayvalue(13)='Sat';
dayvalue(14)='Sun';
dayvalue(15)='Mon';
dayvalue(16)='Tue';
dayvalue(17)='Wed';
dayvalue(18)='Thu';
dayvalue(19)='Fri';
dayvalue(20)='Sat';
dayvalue(21)='Sun';
dayvalue(22)='Mon';
dayvalue(23)='Tue';
dayvalue(24)='Wed';
dayvalue(25)='Thu';
dayvalue(26)='Fri';
dayvalue(27)='Sat';
dayvalue(28)='Sun';
dayvalue(29)='Mon';
end
for n =1:daycount
S(n) = struct('month',monthval,'date',datevalue(n),'day',dayvalue(n));
end
return;
end
if i==1||i ==3||i==5||i == 7||i==8||i ==10||i ==12
if i == 1
monthval = 'January';
daycount = 31;
for n = 1:31
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Fri';
dayvalue(2)='Sat';
dayvalue(3)='Sun';
dayvalue(4)='Mon';
dayvalue(5)='Tue';
dayvalue(6)='Wed';
dayvalue(7)='Thu';
dayvalue(8)='Fri';
dayvalue(9)='Sat';
dayvalue(10)='Sun';
dayvalue(11)='Mon';
dayvalue(12)='Tue';
dayvalue(13)='Wed';
dayvalue(14)='Thu';
dayvalue(15)= 'Fri';
dayvalue(16)='Sat';
dayvalue(17)='Sun';
dayvalue(18)='Mon';
dayvalue(19)='Tue';
dayvalue(20)='Wed';
dayvalue(21)='Thu';
dayvalue(22)='Fri';
dayvalue(23)='Sat';
dayvalue(24)='Sun';
dayvalue(25)='Mon';
dayvalue(26)='Tue';
dayvalue(27)='Wed';
dayvalue(28)='Thu';
dayvalue(29)='Fri';
dayvalue(30)='Sat';
dayvalue(31)='Sun';
end
if i == 3
monthval = 'March';
daycount = 31;
for n = 1:31
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Tue';
dayvalue(2)='Wed';
dayvalue(3)='Thu';
dayvalue(4)='Fri';
dayvalue(5)='Sat';
dayvalue(6)='Sun';
dayvalue(7)='Mon';
dayvalue(8)='Tue';
dayvalue(9)='Wed';
dayvalue(10)='Thu';
dayvalue(11)= 'Fri';
dayvalue(12)='Sat';
dayvalue(13)='Sun';
dayvalue(14)='Mon';
dayvalue(15)='Tue';
dayvalue(16)='Wed';
dayvalue(17)='Thu';
dayvalue(18)='Fri';
dayvalue(19)='Sat';
dayvalue(20)='Sun';
dayvalue(21)='Mon';
dayvalue(22)='Tue';
dayvalue(23)='Wed';
dayvalue(24)='Thu';
dayvalue(25)='Fri';
dayvalue(26)='Sat';
dayvalue(27)='Sun';
dayvalue(28)='Mon';
dayvalue(29)='Tue';
dayvalue(30)='Wed';
dayvalue(31)='Thu';
end
if i == 5
monthval = 'May';
daycount = 31;
for n = 1:31
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Sun';
dayvalue(2)='Mon';
dayvalue(3)='Tue';
dayvalue(4)='Wed';
dayvalue(5)='Thu';
dayvalue(6)= 'Fri';
dayvalue(7)='Sat';
dayvalue(8)='Sun';
dayvalue(9)='Mon';
dayvalue(10)='Tue';
dayvalue(11)='Wed';
dayvalue(12)='Thu';
dayvalue(13)='Fri';
dayvalue(14)='Sat';
dayvalue(15)='Sun';
dayvalue(16)='Mon';
dayvalue(17)='Tue';
dayvalue(18)='Wed';
dayvalue(19)='Thu';
dayvalue(20)='Fri';
dayvalue(21)='Sat';
dayvalue(22)='Sun';
dayvalue(23)='Mon';
dayvalue(24)='Tue';
dayvalue(25)='Wed';
dayvalue(26)='Thu';
dayvalue(27)='Fri';
dayvalue(28)='Sat';
dayvalue(29)='Sun';
dayvalue(30)='Mon';
dayvalue(31)='Tue';
end
if i == 7
monthval = 'July';
daycount = 31;
for n = 1:31
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Fri';
dayvalue(2)='Sat';
dayvalue(3)='Sun';
dayvalue(4)='Mon';
dayvalue(5)='Tue';
dayvalue(6)='Wed';
dayvalue(7)='Thu';
dayvalue(8)='Fri';
dayvalue(9)='Sat';
dayvalue(10)='Sun';
dayvalue(11)='Mon';
dayvalue(12)='Tue';
dayvalue(13)='Wed';
dayvalue(14)='Thu';
dayvalue(15)= 'Fri';
dayvalue(16)='Sat';
dayvalue(17)='Sun';
dayvalue(18)='Mon';
dayvalue(19)='Tue';
dayvalue(20)='Wed';
dayvalue(21)='Thu';
dayvalue(22)='Fri';
dayvalue(23)='Sat';
dayvalue(24)='Sun';
dayvalue(25)='Mon';
dayvalue(26)='Tue';
dayvalue(27)='Wed';
dayvalue(28)='Thu';
dayvalue(29)='Fri';
dayvalue(30)='Sat';
dayvalue(31)='Sun';
end
if i == 8
monthval = 'August';
daycount = 31;
for n = 1:31
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Mon';
dayvalue(2)='Tue';
dayvalue(3)='Wed';
dayvalue(4)='Thu';
dayvalue(5)='Fri';
dayvalue(6)='Sat';
dayvalue(7)='Sun';
dayvalue(8)='Mon';
dayvalue(9)='Tue';
dayvalue(10)='Wed';
dayvalue(11)='Thu';
dayvalue(12)='Fri';
dayvalue(13)='Sat';
dayvalue(14)='Sun';
dayvalue(15)='Mon';
dayvalue(16)='Tue';
dayvalue(17)='Wed';
dayvalue(18)='Thu';
dayvalue(19)='Fri';
dayvalue(20)='Sat';
dayvalue(21)='Sun';
dayvalue(22)='Mon';
dayvalue(23)='Tue';
dayvalue(24)='Wed';
dayvalue(25)='Thu';
dayvalue(26)='Fri';
dayvalue(27)='Sat';
dayvalue(28)='Sun';
dayvalue(29)='Mon';
dayvalue(30)='Tue';
dayvalue(31)='Wed';
end
if i == 10
monthval = 'October';
daycount = 31;
for n = 1:31
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Sat';
dayvalue(2)='Sun';
dayvalue(3)='Mon';
dayvalue(4)='Tue';
dayvalue(5)='Wed';
dayvalue(6)='Thu';
dayvalue(7)= 'Fri';
dayvalue(8)='Sat';
dayvalue(9)='Sun';
dayvalue(10)='Mon';
dayvalue(11)='Tue';
dayvalue(12)='Wed';
dayvalue(13)='Thu';
dayvalue(14)='Fri';
dayvalue(15)='Sat';
dayvalue(16)='Sun';
dayvalue(17)='Mon';
dayvalue(18)='Tue';
dayvalue(19)='Wed';
dayvalue(20)='Thu';
dayvalue(21)='Fri';
dayvalue(22)='Sat';
dayvalue(23)='Sun';
dayvalue(24)='Mon';
dayvalue(25)='Tue';
dayvalue(26)='Wed';
dayvalue(27)='Thu';
dayvalue(28)='Fri';
dayvalue(29)='Sat';
dayvalue(30)='Sun';
dayvalue(31)='Mon';
end
if i == 12
monthval = 'December';
daycount = 31;
for n = 1:31
datevalue(n) =n;
dayvalue(n)=strings(1);
end
dayvalue(1)='Thu';
dayvalue(2)='Fri';
dayvalue(3)='Sat';
dayvalue(4)='Sun';
dayvalue(5)='Mon';
dayvalue(6)='Tue';
dayvalue(7)='Wed';
dayvalue(8)='Thu';
dayvalue(9)= 'Fri';
dayvalue(10)='Sat';
dayvalue(11)='Sun';
dayvalue(12)='Mon';
dayvalue(13)='Tue';
dayvalue(14)='Wed';
dayvalue(15)='Thu';
dayvalue(16)='Fri';
dayvalue(17)='Sat';
dayvalue(18)='Sun';
dayvalue(19)='Mon';
dayvalue(20)='Tue';
dayvalue(21)='Wed';
dayvalue(22)='Thu';
dayvalue(23)='Fri';
dayvalue(24)='Sat';
dayvalue(25)='Sun';
dayvalue(26)='Mon';
dayvalue(27)='Tue';
dayvalue(28)='Wed';
dayvalue(29)='Thu';
dayvalue(30)='Fri';
dayvalue(31)='Sat';
end
for n =1:daycount
S(n) = struct('month',monthval,'date',datevalue(n),'day',dayvalue(n));
end
end

请先登录,再进行评论。


Radoslav Gagov
Radoslav Gagov 2017-4-10
Here is how I dealed with the Problem:
m = year2016(n)
if length(n) ~= 1 || n < 1 || n >12 || n ~= round(n)
year = []; m = year;
else
year = [];
a{1} = 'Mon'; a{2} = 'Tue'; a{3} = 'Wed'; a{4} = 'Thu';
a{5} = 'Fri'; a{6} = 'Sat'; a{7} = 'Sun';a{8} = 'Mon';
a{9} = 'Tue'; a{10} = 'Wed'; a{11} = 'Thu';
a{12} = 'Fri'; a{13} = 'Sat'; a{14} = 'Sun';
if n == 1 || n == 3 || n==5 || n==7 || n==8 || n==10 ||
n==12
for i = 1 : 31
p = mod(i,7) ;
if p == 0
p = 7;
end
year(1).m(i).month = 'January';
year(1).m(i).date = i;
year(1).m(i).day = a{p+4};
year(3).m(i).month = 'March';
year(3).m(i).date = i;
year(3).m(i).day = a{p+1};
year(5).m(i).month = 'May';
year(5).m(i).date = i;
year(5).m(i).day = a{p+6};
year(7).m(i).month = 'July';
year(7).m(i).date = i;
year(7).m(i).day = a{p+4};
year(8).m(i).month = 'August';
year(8).m(i).date = i;
year(8).m(i).day = a{p};
year(10).m(i).month = 'October';
year(10).m(i).date = i;
year(10).m(i).day = a{p+5};
year(12).m(i).month = 'December';
year(12).m(i).date = i;
year(12).m(i).day = a{p+3};
end
end
if n == 4 || n == 6 || n==9 || n==11
for i = 1 : 30
p = mod(i,7) ;
if p == 0
p = 7;
end
year(4).m(i).month = 'April';
year(4).m(i).date = i;
year(4).m(i).day = a{p+4};
year(6).m(i).month = 'June';
year(6).m(i).date = i;
year(6).m(i).day = a{p+2};
year(9).m(i).month = 'September';
year(9).m(i).date = i;
year(9).m(i).day = a{p+3};
year(11).m(i).month = 'November';
year(11).m(i).date = i;
year(11).m(i).day = a{p+1};
end
end
if n == 2
for i = 1 : 29
p = mod(i,7) ;
if p == 0
p = 7;
end
year(2).m(i).month = 'February';
year(2).m(i).date = i;
year(2).m(i).day = a{p};
end
end
m = year(n).m;
end
end
  1 个评论
Stephen23
Stephen23 2017-4-10
编辑:Stephen23 2017-4-10
Some observations:
  • This code is much more complicated than it needs to be. More code -> harder to check and more bugs.
  • Do not write code that already exists and does what you need, particularly when it is tested and more optimized than what you can write yourself. The date functions datenum, etc, already convert and give the correct date/day values, so there is no point in replicating this using loops, etc.
  • This code generates multiple months, even though only one of them is output. Not an efficient use of memory.
  • Uses loops, and expands the structures on each loop iteration. Not an efficient use of memory.
  • Uses magic numbers (these are the days per month): code should not use magic numbers without adequate explanation.
  • Defines a in a very strange way. Much simpler: a = {'Mon','Tue',...}.
  • Uses more than fourteen times as many lines of code as my answer.
  • Is nearly thirty times slower than my answer (1000 repetitions):
Elapsed time is 1.26207 seconds. % my answer
Elapsed time is 34.606 seconds. % this answer
Hopefully other beginners will not use this as an example to learn MATLAB from.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by