Mechatronics Laboratory

Matlab Tips & Tricks

Back to Lab Resources Home

Back to Mechatronics Lab Home

General Tips/Tricks

Vectorize:

Matlab code using vectorized code runs MUCH faster than "for loop" code.

Plotting Comma Delimited Data:

Use the function mfcsvread.m on the ME447 home page.  This m-file assumes comma delimited data with the 1st row containing the colum titles.  Spaces or special characters are not allowed.  However once you load the csv file the column labels are member variables of the input data structure.

Dynamic & Multi-line Plot Titles:

Often time it is useful to add dynamic variables (computed in Matlab) to plot titles that may necessitate them to occupy several lines rather than extend off the edges of the plot. This is done by passing "title" a structure with an array of text for each structure member.  Each structure member will be treated as the next line.  You can use "num2str" to create text string of matlab numeric values that are  concantenated on the end of defined strings by enclosing the two in the array brackets [ ].

title( { 'My Response Plot', ...
['K_c = ', num2str(Kc), '\delta_c = ',num2str(dc)], ...
['Fs = ', num2str(fs),', Position Goal = ',num2str(r) ] } )

Control System Toolbox Tricks:

Change the Bode plot properties:

After calling bode(mysystem) the default units are radians/sec for the frequency axes, dB for the magnitude axis, and degrees for the phase axis.  You may want to change these.  This lab commonly uses Hz for the frequency axis, Absolute for the magnitude axis and the default degrees for the phase axis.

So on to the trick:

% call bode
bode(mysystem) % generates bode plot with default properties
h = gcr; % Get the handle for the "plot object root"
% which is a structure containing the plot properties
% Set frequency axis to Hz
h.AxesGrid.Xunits = 'Hz';
% Set mag axis to abs & phase axis to deg
h.AxesGrid.Yunits = {'abs','deg'};
% Turn on the grid
grid on; % or h.AxesGrid.Grid = "on"

Alternatively you can use bodeplot to get the plot handle and use "getoptions" and "setoptions"

h = bodeplot(mysystem); % Do the bode & return the plot handle
% Change units to Hz, set magnitude to absolute,
h = bodeplot(G_filter)
opt = getoptions(h) % Get the list of options we can change
setoptions(h,'PhaseWrapping','on') % send this option to the plot directly
opt.PhaseWrapping = 'on'; % change the local structure containing the options
opt.MagUnits = 'abs';
opt.FreqUnits = 'Hz';
opt.Grid = 'on';
setoptions(h,opt); % Send the option settings to the plot

this trick is pulled from here

Matlab Floating Point to Fixed Point Conversion Scripts:

These are all written as functions & need to be in the current matlab working directory, or matlab search path

Float2Qpt8 - convert a float to a signed 8-bit fixed point number in Q[QI.QF] format

Float2Qpt16 - convert a float to a signed 16-bit fixed point number in Q[QI.QF] format

Float2Qpt32 - convert a float to a signed 32-bit fixed point number in Q[QI.QF] format

Float2QptSWL - convert a float to a minimum Standard Word Length fixed point number returning the Q-point format in in Q[QI.QF] format

Learning Matlab:

Matlab Control Tutorials - Carnagie Mellon / University of Michigan

Matlab General Tutorial - Arizona State University

Matlab Command Quick Reference -  Arizona State University

If you have questions check resources included on the Mechatronics Lab AVR Resources page or Other Atmel AVR Resources.

Back to Mechatronics Lab Home

Last updated 10/05/2013 05:18 PM