matlab
? @raffle_snaffle
@jim_thompson5910
what's your question @raffle_snaffle ?
how do you extract certain years out of a large matrix using the find command?
which page are you on?
page 3?
page 2 of 4 bottom of the page where I need to find a precip_2013 = find()
wait hold on think i figured it out.
ok post what you get
precip_2013 = find((gcdata2 < 2014) & (gcdata2 < 2015) & (gcdata2 < 2016))
okay maybe not
you just want 2013, so why not say `indices = find(year == 2013)` recall that you stored all the year values in the `year` vector. The stored indices will house the addresses of the rows where the precipitation data corresponds to 2013
btw `(gcdata2 < 2014) & (gcdata2 < 2015) & (gcdata2 < 2016)` is very inefficient because saying x < 2014 AND x < 2015 AND x < 2016 is the same as x < 2014 even then, it's better to just say x == 2013 to get that specific year you want
I forgot about the year vector.
you have to access just the year vector or else you may pick up on data that has 2013 in it that isn't a year value. But that seems unlikely. Even then, it's a good idea to pull the data apart like this
% finds data for precipitation precip_2013 = find(year == 2013); year(precip_2013);
so that will just spit out a bunch of 2013's
Yes, I would agree. But is there a way to extract just 2013 from the gcdata2 = [year', month', day', gcdata1]
can i see your m file?
i forget how gcdata1 is set up
clc; clear % clears command and workspace window load('gcdata'); % loads gcdata format shortg; % formats gcdata % imports excel file into Matlab gcdata = xlsread('govt_camp.xlsx'); % new matrix B, eliminates last two columns Ngcdata = gcdata(:, [1:end - 2]); % converting first column into a string [m, n] = size(Ngcdata); for k = 1:1:m DateString = sprintf('%d',Ngcdata(k,1)); % converts the date value from a number to string year(k) = str2double(DateString(1:4)); % reads off the first 4 digits of DateString and stores in year matrix month(k) = str2double(DateString(5:6)); % reads off next two digits day(k) = str2double(DateString(7:8)); % reads off next one digit end gcdata1 = Ngcdata(:, [2:end]); % removes first colum in Ngcdata gcdata2 = [year', month', day', gcdata1]; % new matrix gcdata2 % finds data for precipitation precip_2013 = find(year == 2013); year(precip_2013);
ok right, first column cut off
which column holds the precip data?
it's column 4 column1 = year column2 = month column3 = day column4 = "Precipitation (inches)" column5 = "Snow Depth (inches)" column6 = "Snowfall (inches)"
so you'll look through just `gcdata2(:,4)` basically every row and column4
here's how I'd do it % finds indices (aka address) for precip data in 2013 precip_indices_2013 = find(year == 2013); % store just the precipitation values in a temporary matrix temp = gcdata2(:,4) % pull out the precipitation values that correspond to year 2013 % uses the indices found previously precip_2013 = temp(precip_indices_2013) that should give you a vector `precip_2013` of precipitation values for the year 2013
I guess you could rename `temp` to `precipitation_values` to be more specific
okay well my precip_2013 vector doesn't match excels
what do you mean?
% finds data for precipitation precip_indices_2013 = find(year == 2013); % locates the indices for years year(precip_indices_2013); % extracts 2013 years from year vector temp_v = gcdata(:,4) % temporar vector for precip measured in inches precip_2013 = temp_v(precip_indices_2013); % actual precip_2013 values precip_2013 = 0 0 0 0 0 0 1 0.1 0 6 2 0 0 0 0 0
Those are the first couple values of the output, but look at the excel file.
you're accessing the wrong gcdata matrix
my bad lol
somehow you ended up in the wrong column. You accessed column 6 instead of column 4
Okay it's fixed
okay I should be able to do the next following steps. It wants me to find the precip for 2014, 2015, and 2016 as well.
btw you don't need `year(precip_indices_2013);` in your code
yeah I guess you are right. I was using it to check something though.
well you suppressed the output so it doesn't do anything. I guess beforehand you didn't have the semicolon at the end
I see what you mean though
I will leave it and suppress the output.
we are using a bar graph right? My bar graph looks similar to the image displayed on the lab. Also I am working on getting a title, x label, and y label on the graph too.
in your pdf, it looks like a line graph
nvm I got the subplot to work correctly with the correct title, x label, and y label
% finds 2013 precip data precip_indices_2013 = find(year == 2013); % locates the indices for years year(precip_indices_2013); % extracts 2013 years from year vector temp_13 = gcdata2(:, 4); % temporar vector for precip measured in inches precip_2013 = temp_13(precip_indices_2013); % actual precip_2013 values from col. 4 bar2013 = bar(precip_2013); subplot(2, 2, 1); title('2013 precipitation (in)'); xlabel('years (t)'); ylabel('snowfall (in)'); % find 2014 precip data precip_indices_2014 = find(year == 2014); year(precip_indices_2014); temp_14 = gcdata2(:, 4); precip_2014 = temp_14(precip_indices_2014); bar2014 = bar(precip_2014); subplot(2, 2, 2); title('2014 precipitation (in)'); xlabel('years (t)'); ylabel('snowfall (in)');
my subplots are reversed and the output is weird looking. Does the code look correct or do I have something backwards? This is for 2013 and 2014.
why are you doing a bar graph?
I see a line graph on page 2
i am reading the lab. I don't see where it says "use a line graph".
well if I am using a plot(x, y) command I know why my subplots won't work correctly.
I'm looking at the examples given
top of page 2 `Matlab Figure 1: You will create a Matlab Figure 1 that will end up looking like this document’s Figure 1.` then it shows a bunch of line graphs
okay, thanks. I am trying to get the subplots to work.
subplot need to be before or after plot(x,y)?
before plot
subplot sets up the empty slots for the plots to go in
% finds 2013 precip data precip_indices_2013 = find(year == 2013); % locates the indices for years year(precip_indices_2013); % extracts 2013 years from year vector temp_13 = gcdata2(:, 4); % temporar vector for precip measured in inches precip_2013 = temp_13(precip_indices_2013); % actual precip_2013 values from col. 4 subplot(2, 2, 1); plot(year, temp_13); title('2013 precipitation (in)'); xlabel('years (t)'); ylabel('snowfall (in)'); % find 2014 precip data precip_indices_2014 = find(year == 2014); year(precip_indices_2014); temp_14 = gcdata2(:, 4); precip_2014 = temp_14(precip_indices_2014); subplot(2, 2, 2); plot(year, temp_14); title('2014 precipitation (in)'); xlabel('years (t)'); ylabel('snowfall (in)');
How does this look? It seems to run fine.
you didn't set up the 2013 plot correctly
you created precip_2013 just fine, but you forgot to use it in the plot
same for 2014
I used temp_13 and temp_14 as my y in plot(x,y) because we are plotting snowfall with respect to years I thought.
temp_13 and temp_14 is my data for precip measured in inches.
here's how I'd do the 2013 plot % finds indices (aka address) for precip data in 2013 precip_indices_2013 = find(year == 2013); % store just the precipitation values in a temporary matrix temp = gcdata2(:,4); % pull out the precipitation values that correspond to year 2013 % uses the indices found previously precip_2013 = temp(precip_indices_2013); % pulls out the corresponding day values for the year 2013 days_2013 = day(precip_indices_2013); % set up plot subplot(2, 2, 1); plot(days_2013, precip_2013); title('2013 Daily Precipitation (in)'); xlabel('day of year'); ylabel('inches');
why can't I use my code? lol
you can. I'm just showing you how I did it. The way you set it up, you put the year along the x axis when it should be the day value. Also when you wrote `plot(year, temp_13);` you are using ALL the precip values when you want the 2013 precip values only
so you're mixing up variables
I am thinking.
take all the time you need
day isn't a vector? Don't you mean years
I see the lab says days
remember how we broke up the first column into 3 columns? years, months, days each column is a vector
hmm it's either `day` or `days`. I forget what we named it
ugh i see i see lol
hmm now that I think about it, the days vector won't work. Because after 31, the days shouldn't wrap back to 1. It should keep counting up
okay good because my graph looks really weird.
hmm let me think
I am just formatting my code
we have to create a vector starting from 1 and going through to 365 to represent the 365 days of the year. So this vector will be a 365x1 matrix
ok
this might do the trick for k=1:1:365 day_of_year(k) = k; end though for 2016 it won't because 2016 isn't over yet. The graph is like 100 days into 2016
I see what you are doing there. Why couldn't I use k = 1:1:365;
you mean for the `for` loop?
day_of_year is just a vector right?
yes spanning from 1 to 365 (incrementing by 1)
why do we need to use a for loop though?
oh you mean day_of_year = 1:1:365; ??
yeahhh
hmm that might actually work, let me test
yes it works, it makes a 1x365 matrix
so you may have to transpose that
okay, sorry I was thinking.
day_of_year = [1:1:365]' will transpose it if you need it to be a column vector
what is our y in plot(x,y) ?
the precip data still
Join our real-time social learning platform and learn together with your friends!