How to request additional data from the user in a function
古いコメントを表示
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 件のコメント
Take a look at this example:
The output to your problem would be something similar to "patient array" in the above example, where there will be N elements based on the input you provide. For example, if the input is 2 (February), there are 29 days in Feb 2016, so the struct array will have 29 elements, where each element corresponds to each day of the month.
This should be enough to get you started.
BTW, the question doesn't seem to state that you need to request additional data from within the function. The function simply takes in one input argument, and that's it.
DJ V
2016 年 12 月 7 日
" It appears to require a second query within the function."
No, have a look at the example that you have been given. Selecting the day occurs by indexing into the output variable m, not by an input argument to the function. The output variable needs to contain all days for the selected month.
DJ V
2016 年 12 月 7 日
Geoff Hayes
2016 年 12 月 7 日
That's a lot of code and a lot more than necessary for this homework assignment. Start with a simple example starting with January. All you should need is to know how many days there are in each month (for 2016) and which day was January 1rst, 2016 (it was a Friday).
As for your error, put a breakpoint at the line that is throwing the error and then run your code. When the debugger pauses at this line, what can you say about dayvalue? Does your assignment make sense given how it has been initialized? Look closely at the line
dayvalue=(31);
What does this mean to you? All that you are doing is assigning 31 to this variable. I suspect that you are trying to create an array of 31 elements but this is not the way to do so. If you know how many days are in the month, then initialize your structure array as
numDaysInMonth = 31;
S(numDaysInMonth) = struct('month','','date',0,'day','');
Now iterate over each element in the array and update the month, date, and day fields.
Geoff Hayes
2016 年 12 月 8 日
Ummm...did we just do DJ's homework (again)?
Stephen23
2017 年 4 月 10 日
@DJ V: you need to learn how to debug code. Start here:
and also read this:
Lauren Jablonowski
2018 年 5 月 10 日
編集済み: Lauren Jablonowski
2018 年 5 月 10 日
I'm having an issue where I'm getting an error message when the tested input is zero. It is supposed to return an empty struct in this case, but I cannot get an if statement to work within this construct. Thanks! My code is below:
function MDD = year2016(m)
for m = 1:12
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'};
MDD = struct('day',DC(DN),'date',num2cell(1:numel(VN)));
[MDD(:).month] = deal(MC{m});
end
for m = 0
MDD = struct();
end
end
Stephen23
2018 年 5 月 10 日
@Lauren Jablonowski: none of your for loops make any sense. Get rid of them. Use one if statement.
Lauren Jablonowski
2018 年 5 月 18 日
@Stephen Cobeldick: thanks! with the if statement and isscalar function I was able to fully solve this.
採用された回答
その他の回答 (3 件)
Geoff Hayes
2016 年 12 月 7 日
1 投票
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
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
2016 年 12 月 8 日
編集済み: Walter Roberson
2016 年 12 月 8 日
Walter Roberson
2016 年 12 月 8 日
You are not storing anything into m with that code. You have no variable named m anywhere in your posted code.
DJ V
2016 年 12 月 8 日
DJ V
2016 年 12 月 8 日
Walter Roberson
2016 年 12 月 8 日
See what I posted about cell arrays.
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 件のコメント
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.
カテゴリ
ヘルプ センター および File Exchange で Time Series Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!