Filter löschen
Filter löschen

Hey guys, I am really lost and I don't know where to start with this question

2 Ansichten (letzte 30 Tage)
Write a function called year2016 that returns a row-vector of struct-s whose elements correspond to the days of a month in 2016 as specified by the input argument. If the input is not an integer between 1 and 12, the function returns the empty array. Each struct should contain three fields with these (exact) field names: “month”, “date”, and “day” (all lower case).  The month field must contain a string with the name of the month (first letter capitalized).  The date field must contain a scalar specifying the day of the month.  The day field must contain the three-letter abbreviation of the day chosen from this list: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'. For example, here is a call of the function followed by a command that shows the seventh element of the struct array that is returned by the function: >> m = year2016(2); >> m(7) ans = month: 'February' date: 7 day: 'Sun'
I have been trying for hourse. I am not sure how to make it so, that when i call the function afther that i have the option to write m again but this time give it a value and to become the day at the end. I would be really gratefull. It should look like this :
m = year2016(4)
m =
1×30 struct array with fields:
month
date
day
>> m(1)
ans =
struct with fields:
month: 'April'
date: 1
day: 'Fri'
and myone one looks like this:
m = year2016(1)
m =
struct with fields:
month: 'January'
day: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31]
Here is my code now, i have changed it more than 10 times so it doesnt really show my closest attempt probably, but would give some idea why i am not doing it right.
function [m] = year2016( x )
field1 = 'month'; field2 = 'date'; field3 = 'day';
m1 = 'January'; m2 = 'February'; m3 = 'March';
m4 = 'April'; m5 = 'May'; m6 = 'June';
m7 = 'July'; m8 = 'Augustm'; m9 = 'September';
m10 = 'October'; m11 = 'November'; m12 = 'December';
d1 = {1,2,3}; d2 = [1:30]; d3 = [1;2;3];
m = struct(field1,m1,field2,d3);
%,m2,d1,m3,d3,m4,d2,m5,d3);
end

Akzeptierte Antwort

Stephen23
Stephen23 am 6 Apr. 2017
Bearbeitet: Stephen23 am 6 Apr. 2017
This code does the job (copied from my answer to this question, so this code is already in the public domain)
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
>> m = year2016(4); m(1)
ans =
day = Fri
date = 1
month = April
  6 Kommentare
Radoslav Gagov
Radoslav Gagov am 10 Apr. 2017
Hmmm.How do you know that DN = 1+mod(VN-3,7); is going to be the Daynumber.
I havent really used deal,num2cell and cell so much so its a bit harder to understand it. I think afther reading the explanations of all the fuctions i understood it almost except for the
[out(:).month] = deal(MC{m});
its like we crated a struct with the date and day but then combine it with a month or i dont know. Probably i need a little more experience to understand that, but still thank you.
Stephen23
Stephen23 am 10 Apr. 2017
Bearbeitet: Stephen23 am 10 Apr. 2017
"How do you know that DN = 1+mod(VN-3,7); is going to be the Daynumber?"
Trial-and-error. It is easy to find the serial date number for today (use the function now). Obviously we need mod 7 (because of seven days per week), and the offset can be figured out quickly with a few examples. The plus one is because I want to use this as an index, and MATLAB indices start from one (not zero).
"its like we created a struct with the date and day"
Exactly correct. The date and day make a structure with 28/29/30/31 elements. But notice that the month name is the same all month long! And note that MC{m} is just one month name. Therefore we want to allocate one month name to all 28/29/30/31 elements of the structure. This is exactly what
[out(:).month] = deal(MC{m});
does. The LHS is all 28/29/30/31 elements of out in a comma-separated list, and the deal allocates our single month name to every member of that list (creating the month field too).
See my answer to this question which explains it a bit differently.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jose alberto Guzmán Torres
Jose alberto Guzmán Torres am 30 Nov. 2017
Que pasa para cuando los valores de m se salen del intervalo de 1 a 12?? ¿Como solucionas eso?
  1 Kommentar
Stephen23
Stephen23 am 30 Nov. 2017
Bearbeitet: Stephen23 am 30 Nov. 2017
Read the assignment again: "If the input is not an integer between 1 and 12, the function returns the empty array."
The answer is right there in the assignment: use IF.
The answer is also in the discussion here:

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by