Matlab final
What does caller do when using refreshdata?
'caller' tells Matlab hey this variable is in the function file
How are mesh plots and surface plots different?
- Mesh plots are joined by colored lines (holes in the grid) mesh(X,Y,Z) - Surface plots have colored surfaces (no holes in the grid)
How do you use the refresh data function when updating a plot? What are the a actual steps?
1) Create a plot, and plot handle 2) Set the data sources for your plot using the handle 3) Update your data for your plot within a for loop 4) Call refreshdata 5) Call drawnow to redraw the plot x = linspace(1,100); yplot = zeros(1,length(x)); plotHandle = plot(x,yplot); set(plotHandle,'XDataSource','x'); set(plotHandle,'YDataSource','yplot'); for i = 1:length(x) yplot(i) = randi(100); refreshdata(plotHandle,'caller'); drawnow; end
What are the steps to making a movie?
1. graph data usually plot in a for loop so that each time more and more plots are added. (use hold on and hold off) 2. make a frame of it usually inside of the for loop; make the frame by assigning the movie to a variable and calling getframe M(i) = getframe 3. change the data Usually data is changed within the for loop 4. repeat yValue = zeros(1,30); yMean = zeros(1,30); for i = 1:30 yValue(i) = rand()*100; yMean(i) = sum(yValue)/i; hold on plot(1:i,yValue(1:i),'ko-'); plot(yMean(1:i)); hold off axis([0 30 0 100]); M(i) = getframe; end movie(M)
What are the two ways to animate plots?
1. make a movie 2. update a plot
What is a plothandle?
A plothandle is a way of accessing something by a "nickname" We use plothandles when updating plots
What kinds of data can attributes hold?
Attributes can hold all types of data, numbers, strings, arrays, etc.
What does spline and pchip do?
Basically, they encompass the characteristics of both polyfit and polyval together. spline: creates a cubic curve between each node (x value) with the added condition that the first and second derivatives are continuous throughout the spline. -Spline will interpolate and may give extrema (mins or maxs) at interpolated data points pchip: creates a piece-wise cubic curve between each node that maintains the shape and monotonicity of data. -Pchip will interpolate and only give extrema (mins or maxs) at known data points, not interpolated data points
Why are these strings in curly braces and separated by commas? {'This text is on line 1', 'This text is on line 2','This text is on line 3',...}
Because we want each string to be on separate lines
What does a mesh plot look like?
Carpet
What is the difference between the function char and strvcat?
Char will concatenate everything vertically, including empty strings, but strvcat will not include empty strings c = char('first', 'second','', 'fourth') c = first second fourth c = strvcat('first', 'second','', 'fourth') c = first second fourth
What will this code produce? a = ['Concat'; 'Concat']; b = ['enate']; c = strcat(a,b)
Concatenate Concatenate Basically what this does is pairs string b with both rows of string a
What does content addressing mean and what must you use?
Content addressing refer to the information stored within the cell. You must use curly brackets {}
What is dot notation used for?
Dot notation is used to access the attributes of a structure. So say for instance you create an empty structure and now want to fill that empty structure with the appropriate things for each attribute, you would do dot notation for each attribute. Formatting is the name of the structure (.) the attribute name. For example: student.name = 'Ray' This will fill the attribute for name with the string ray, basically creating one student. This line of code accesses the student structure, and then accesses the name attribute and assigns it a string value.
What are the two ways you can update plots?
Either you can refresh data and the drawnow, all of this being in a for loop but with the plot handle and set functions outside for loop, or you can just call a for loop with set functions, use XData and YData and remember to set plothandle outside of for loop
Does an upper have zeros above or below main diagonal?
Has zeros belo.
What function do you use to call multiple plots on a single axes?
Hold command will allow you to put the plotting on hold because you are not done with it yet To be safe, specify hold on or hold off
How do you plot polynomial fits?
In order to plot polynomial fits, you must first find the fit of some original data x and y. so in order to do this, you would do something like myfit = polyfit(x,y,orderfit) Polyfit uses the original x and y values plus some order of the polynomial to return a vector of coefficients. Next, we must evaluate our polyfit using polyval. create some x values with linspace and then use those. polyval will return a vector of y valuates. It would look something like this yplotfit = polyval(myfit,x) This basically means, evaluate my polyfit at these x values. once you have y values for your fit, you are good to go and can plot plot(x,yplotfit,'m*') plot always includes the x and y and the parameters. remember to include titles, x labels, ylabels, and a legend if needed.
What is this code looking for? '\$\d*\.\d*'
Looking for an actual $ sign followed by any number of digits, followed by an actual period, followed by any number of digits
What is this code looking for? '\. [A-Z]'
Looking for the beginning of a sentence.
What function do you call to play the movie and how do you call it?
Movie(movie name, numLoops)
What is ODE45 used for?
ODE means Ordinary Differential Equation [t,y] = ode45(@fun, tspan, yzero) The output of ode45() are two vectors t and y which contain data points (usually for plotting) • ode45() chooses the length of t and y by itself when given a range to produce a nice plot.
What is subplotting? How does it work?
Subplotting is a way for you to plot multiple plots in one figure window. You subplot by calling the subplot function followed by the data that should occupy this spot. The way you call it is subplot(m,n,i) this specifies the row m, column n, and the space you would like it to occupy subplot(2,3,1), plot(x1,y1) subplot(2,3,2), hist(x2,y2) subplot(2,3,3), bar(y3) subplot(2,3,4), barh(y4) subplot(2,3,5), hist(y5) subplot(2,3,6), pie(y6)
What is the default quantifier?
The default quantifier is the greedy quantifier, this takes in as much of the string that fits the criteria.
What is the symbol for the lazy quantifier and what does it do? give an example.
The lazy quantifier is ? and this Matches as little of the string as possible. Called a lazy quantifier
Usually, what happens as you increase the order of a polyfit?
The polyfit begins to look more and more unusual
What is the possessive quantifier?
The posessive quantifier uses the +
What does this code ask to do? regexp(str,'\w+','match')
This code asks to match any word character as one or more times
What does this code do? fun = 'sqrt'; ev = '(2)'; f = eval([fun ev])
This code basically will evaluate using the given function and the number. Will result in f = 1.4142
What does this code do? regexp(str, 'p.p','match')
This code will look for p and p with any single character between.
What will this code do? X1 = 1:10; Y1 = rand(1,10).*X1; Y2 = rand(1,10).*X1; plot(X1,Y1,'k'); hold on plot(X1,Y2,'m-'); hold off
This code will plot x1 and y1 and then hold on until more data is provided and then plot x1 and y2 on the same plot
What will this code result in? a = ['Concat'; 'Concat']; b = ['enate'; 'enate'; 'enate']; c = strcat(a,b)
This code will result in an error because the number of rows in b is greater than the number of rows in a
What will this code do? regexp(str,'pe')
This code will return the indices of where these combination of letters appear
What does the sprintf function do?
This is basically just a tool to print out strings with floating values
What does this code mean? X = [ 1 2 3 4]; Y = [6 5 7 10]; P = polyfit(X,Y,1) %linear trend (we think) P = 1.4000 3.5000
This is basically trying to find a polynomial fit and using polynomial fit will give back a vector of coefficient vectors. Because p = 1.4 and 3.5, the polynomial fit is y(x) = 1.4x + 3.5 if there were three numbers in the vector, this would be a quadratic equation so basically, first you need x and y data and an estimated order fit, use polyfit(x,y,orderfit) to get vector of coefficients
What does this code do w = ['Concat' 'enate']
This is called string concatenation, and this code will result in w = Concatenate. This is the same as using the strcat function
What does polyder do?
This takes the derivative of a polynomial
What exactly does this code do on a plot? text(X,Y,'Add this text') And what function would be better to use, allowing you to manually choose location with cursor?
This will add text to the graph at coordinate x,y gtext('text') And this only works if you have a figure window open
What will this code do? regexp(str,'p.[cr]','match')
This will search for strings that begin with a p and with any single character between, followed by a c or r
What function do you use to count the number of input arguments? outputs?
Use nargin for number of input arguments and nargout for the number of outputs
What function do you use to search a string for a sub string?
Use the strfind function. str = 'This is my string and, my string is cool'; index = strfind(str,'is') index = 3 6 33
What do you do to create an empty structure?
Use the struct function and add the attributes names as strings and put empty brackets [] after the attribute name because there is no information yet to be stored there. student = struct('name',[],'class',[],'ID',[],'Email',[])
What are the differences between strfind
We can use strfind() to find specific subsets of strings within a string It's search criteria that is built off of a singular expression (hence the name)
How do we change the behavior of modifiers?
We do this by using quantifiers
Why do we use meshgrid? How do you use it?
We must use meshgrid when we are creating a surface plot and Z is a function of x and y First assign values to x and y Then call the meshgrid function on your x and y values and set it to xgrid and ygrid. Then define your z function which should be in terms of xgrid and ygrid. then call surf on xgrid ygrid and z X = -10:0.5:10; Y = -10:0.5:10; [XGrid, YGrid] = meshgrid(X,Y); Z = XGrid.*YGrid - 3*YGrid.^2 + 9*XGrid.^2; % Z(x,y) = xy - 3y^2 + 9x^2 surf(XGrid, YGrid, Z);
What do we use anonymous functions for and how are they setup?
We use anonymous functions to create functions on the fly. you must set it equal to f_x and then the first thing to set is the variables @(x,y)
What do we use to evaluate a polynomial? especially when deaing with polyfits?
We use polyval. The way polyval works is that you design the x values you want this polynomial to be evaluated at and then you do polyval(polyfit name,x values)
How do we vertically concatenate strings?
We use strvcat(a,b).
How is vertical concatenation different from horizontal?
Well we use strcat for horizontal and we use char or strvcat for vertical
What are the requirements for horizontal string concatenation?
Well, both strings must have the same number of rows or one string must only be one row.
After plotting the fit, you are usually asked to to find r-squared values, what will you do?
Well, what you first needs is a polyfit, which you would already have, but lets say you didnt. First, you would get the polyfit of some original data x and y to some order fit: myfit = polyfit(x,y,1) Next, you need to evaluate this polyfit for x values using polyval -yPlotfit = polyval(myfit,x) basically this says evaluate myfit at x then once you have this, you also need the mean of y which is yBar yBar = mean(y) now, we have everything we need to calculate r-squared. ssRes = sum((Y-yfit).^2) ssTot = sum(Y-yBar).^2) rSq = 1 - (ssRes/ssTot)
For the purpose of our class, what is the main difference between XData and XDataSource?
XData is visually faster!
With subplotting, can one plot occupy multiple columns in a row?
Yes, and you simply do this by doing something like subplot(2,3,1), plot(x1,y1) subplot(2,3,2:3), pie(y6) subplot(2,3,4:6), barh(y4)
How do you create a 3D contour and what must you include?
You can create a 3D contour by using the contour3 function and in this function include xgrid, ygrid, z and the number of contours to include
What can you do instead of using the refresh data function?
You can simply create your plothandle and axis outside of your for loop, loop through the number of points and inside the for loop set the new values of x and y and call the set function on x and y, but this time only use 'XData' and 'YData' and variable name also does not go into single quotes because we are accessing an actual variable. You still must call draw now x = 0; y = 0; plotHandle = plot(x,y,'ko-'); axis([1 100 0 100]); for i = 1:100 x = 1:i; y = randi(100,1,i); set(plotHandle,'XData',x); set(plotHandle,'YData',y); drawnow; end This is the way I prefer to do it!
What do you have to do regardless to if you use the refreshdata function or not?
You must always call drawnow If you do not call the drawnow function, you will never update your plot
How do you print out single quotes?
You must use '' ''
When must you use meshgrid?
You must use meshgrid whenever z is a function of x and y
How does the strcat function work?
a= 'can' b='dice' c = strcat(a,b) c = candice This basically allows you to combine strings. Think of concatenate which means to link
in 3D plots, how do we specify axes?
axis([xmin xmax ymin ymax zmin zmax])
Generally, how do we format axis?
either use: axis([minx maxx miny maxy]) axis auto
What are the different ways we can get back to a numerical matrix from the string created array?
eval() str2num() This is the exact opposite of num2str
How do you write this function as an anonymous function? y(x) = 9x + 2
f_x = @(x) 9*x + 2
How do you add a legend to your plot?
legend('Dataset 1','Dataset 2')
How do you save a movie to avi?
movie2avi(variable where movie is stored, 'what you want it saved as.avi)
What function is used for regular 3D points?
plot3() Remember, you can also plot 3D points
How do we plot vector fields?
quiver(tGrid, yGrid, U, V, scale)
What function do you use for regular expressions
regexp
How do you call the regular expressions function?
regexp(str,'pe')
What do you use to do remove an attribute from a structure?
rmfield(structure, 'attributename')
how does the set function for refresh data work?
set(plotHandle,'XDataSource','varname'); set(plotHandle,'YDataSource','varname') XDataSource and YDataSource is case sensitive and must be typed just like this in single quotes. Basically what the set function does is set the appropriate data for x and y in your plot. The data source names and the variable must be in single quotes
How does the strcmpi function work
stringcmpi(variable name,what you want to compare it to. Strcmpi ignores case, just using strcmp will be case sensitive.
How do you do string conversion with numbers that are not integers?
use num2str function
How do you create a surface plot with a contour underneath?
use surfc function
how can we produce a string that contains the correct MATLAB syntax for creating an array of values
using mat2str() which will result in something like this [1 2 3; 4 5 6; 7 8 9]
why do we include 'match' in our regular expression?
we do this because instead of giving back indices, we want the exact character values
How do we convert integers to strings?
we do this using the int2str function
How do you add axis labels and titles to a plot?
xlabel('string') ylabel('string') title('string')
How can we combine all the interpolation techniques into ONE function:
y = interp1(x,y,r,'type') r is the range and type can be any of these: 'nearest' : nearest neighbor interpolation 'linear' : linear interpolation (default) 'spline' : cubic spline interpolation 'pchip' : piece-wise cubit Hermite interpolation
How would I evaluate this function at 2? y_x = @(x) 9*x + 2;
y2 = f_x(2)