Anda di halaman 1dari 8

Matlab Notes

David Doria
December 16, 2008
Contents
1 Matlab Notes 3
2 Startup 3
2.1 Linux Launcher . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Startup Settings . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2.1 Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2.2 Startup Scripts . . . . . . . . . . . . . . . . . . . . . . 3
3 General 3
3.1 File Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Pad a String with Leading Zeros . . . . . . . . . . . . . . . . . 3
3.3 Block Comment . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.4 Clear Specic Variables . . . . . . . . . . . . . . . . . . . . . . 4
3.5 Read Binary Data Files . . . . . . . . . . . . . . . . . . . . . 4
4 Plotting 5
4.1 Color of EZPlot Curves . . . . . . . . . . . . . . . . . . . . . . 5
4.2 Variables in the Title of a Plot . . . . . . . . . . . . . . . . . . 5
4.3 Draw Thick Lines . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.4 Plot Points Instead of Lines . . . . . . . . . . . . . . . . . . . 5
4.5 Solid Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.6 Legend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.6.1 Creating a Legend . . . . . . . . . . . . . . . . . . . . 6
4.6.2 Font Size . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.7 Maximize Figure . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.8 Set Plot Color to an RGB Value . . . . . . . . . . . . . . . . . 6
4.9 Axis Tick Color . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.9.1 Predened Color . . . . . . . . . . . . . . . . . . . . . 6
4.9.2 RGB Color . . . . . . . . . . . . . . . . . . . . . . . . 6
4.10 Axis Background Color . . . . . . . . . . . . . . . . . . . . . . 6
4.11 Figure Background Color . . . . . . . . . . . . . . . . . . . . . 6
4.12 Change Font Size on Plot Labels / Legend . . . . . . . . . . . 7
4.13 Save a Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.13.1 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.13.2 Options . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.14 Non-Standard Color of 3d plot . . . . . . . . . . . . . . . . . . 7
1
4.15 Position Axes on Figure . . . . . . . . . . . . . . . . . . . . . 7
4.16 Fake Axis Labels . . . . . . . . . . . . . . . . . . . . . . . . . 7
2
1 Matlab Notes
For the most part, the Matlab documentation is pretty good. However,
there are plenty of things that I frequently need to do that are not made
particularly easy. Most of these sections are simply How do I ... ?s, but
some are scripts which I use repeatedly from which a lot of information/tricks
can be gained.
2 Startup
2.1 Linux Launcher
In Linux, to run Matlab from a launcher, use the command matlab -desktop.
If you simply run matlab, the splash screen will appear, but Matlab will not
open.
2.2 Startup Settings
2.2.1 Path
The Matlab path is dened in pathdef.m which exists in /../MATLAB/toolbox/local/
2.2.2 Startup Scripts
If startup.m or matlabrc.m is on the Matlab path, it will be run at startup.
3 General
3.1 File Output
fid = fopen(sample_file.txt,rt);
fprintf(fid,%s,last_line);
fclose(fid);
3.2 Pad a String with Leading Zeros
I use this to produce a repeated lename list for slowing down videos with
mplayer.
3
In an output le:
fid = fopen(files.txt,a);
for i = 2:29
for repeat = 1:10
fprintf(fid,%03d.jpg\n,i);
end
end
fclose(fid);
In a string (c style) filename = [file sprintf(%05d, counter) .png]
In a string (Matlab style) filename = [file num2str(counter) .png]
3.3 Block Comment
%{
block
comment
%}
3.4 Clear Specic Variables
clearvars -except var1 var2
3.5 Read Binary Data Files
fid = fopen(week10data.rob,r) % open file
[fname, mode, mformat] = fopen(fid) %Determine file format
%Read binary data - ieee-le is "little endian" format
pos_data = fread(fid, inf, single, ieee-le)
4
4 Plotting
4.1 Color of EZPlot Curves
%change properties of last curve in current figure
%Examples:
% setcurve(color,red)
% setcurve(color,green,linestyle,--)
%Type help plot to see available colors and line styles
function setcurve(varargin)
h=get(gca,children);
set(h(1),varargin{:})
4.2 Variables in the Title of a Plot
title([Iteration: ,num2str(counter)])
OR
set(gca,title,text(string, sprintf(Iteration: %d, counter)))
set(gca, title, text(string, mystring))
%note the first string is the type of text
%do not replace with an actual string!
4.3 Draw Thick Lines
plot(..., LineWidth, LINE_WIDTH);
4.4 Plot Points Instead of Lines
plot(..., LineStyle, o);
4.5 Solid Markers
With Edges: plot(...,MarkerFaceColor, g);
Without Edges: plot(...,MarkerFaceColor,g,Color, g);
5
4.6 Legend
4.6.1 Creating a Legend
legend(title of object1, title of object2, ...);
4.6.2 Font Size
Pax = axes(FontSize, FONT_SIZE);
4.7 Maximize Figure
figure(units,normalized,outerposition,[0 0 1 1]);
4.8 Set Plot Color to an RGB Value
a = plot(x,y);
R = 1; G = 0; B = 1;
set(a,color,[R G B])
4.9 Axis Tick Color
4.9.1 Predened Color
set(gca,XColor,w)
4.9.2 RGB Color
set(gca,YColor,[1 0 1])
4.10 Axis Background Color
set(gca,color,black)
4.11 Figure Background Color
set(gcf,color,black)
6
4.12 Change Font Size on Plot Labels / Legend
Pax = axes(FontSize, FONT_SIZE);
4.13 Save a Figure
4.13.1 Standard
saveas(h,filename.ext)
4.13.2 Options
print -djpeg -r150 NumPoints2.jpg;
4.14 Non-Standard Color of 3d plot
plot3(x, y, z, Color, [r g b]) r, g, and b should take values [0, 1]
4.15 Position Axes on Figure
screensize = get(0, ScreenSize);
pos = [1 screensize(4)/2 screensize(3)/2 screensize(4)/2];
figure(Name, Number Of Points, Position, pos);
a = axes(FontSize, FONT_SIZE, position, [.07 .1 .9 .8]);
%[left bottom width height]
set(gcf, PaperPositionMode, auto);
print -djpeg -r200 NumPoints.jpg;
4.16 Fake Axis Labels
TickLabels = get(gca, YTickLabel);
ticks = get(gca, YTick);
set(gca, YTick, [min(ticks), max(ticks)]);
set(gca, YTickLabel, [0 1]);
7

Anda mungkin juga menyukai