-
Notifications
You must be signed in to change notification settings - Fork 0
/
guide.m
233 lines (154 loc) · 5 KB
/
guide.m
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
%% Just press f5 to run everything, and read the section that has something you need
% Press (ctrl+enter) to run the section your cursor is currently in ( Marked in yellow)
clear all
close all
%% Init
%Just some example-arrays
X1 = [1:100];
Y1 = sin(X1./10);
% The samples have different length
X2 = [1:0.5:100];
Y2= 1.-(1./X2);
disp("Init complete");
%% Basic plot
% All code you write betweeen hold on and hold off
% will apply to the same plot
hold on
% (Optional) Add a title
title("Basic plot");
plot(X1, Y1); % Plot the function
hold off
%% Legends and labels
figure; % Calling figure => Plot/write in a new window
hold on
title("Set Legends and labels with the commands they display");
plot( X1, Y1);
% Set desctiptions on the axis
xlabel("xlabel('text')");
ylabel("ylabel('text')");
% Legend must come after you call plot()
% GIves a name to the plotted lines
%
legend(["legend('text')"]);
hold off
%% Ploting values against each other
figure
hold on
title("Ploting values against each other");
%Methon 1:
%Give the inputs in pairs
% X1 and X2 can have different lengths as long as the length is the
% same for between X1 and Y1 (and so on)
plot(X1, Y1, X2, Y2);
legend(["It is fine to have more plots that legends"]);
% Or more legends than plots (But it causes a warning)
hold off
% %Method 2:
% % Give the X-values first and then a list of all Y-values
% %(Separate with ; )
% plot( X1, [Y1; sin(X1) ] );
%% SUBPLOTS
figure;
hold on
title("Subplots");
% Subplot(n, m, i)
% Divides the window into a grid of n * m plots
% i is the position of the plot (left to right, top to bottom)
subplot(2, 1, 1);
plot(X1, Y1);
% They work just like normal between hold on and hold off
subplot(2,1, 2);
hold on
plot(X2, Y2, X2, -Y2);
legend("Subplot 2");
xlabel("X");
ylabel("Y");
legend(["Multiple subplots", "Title is not working"]);
hold off
hold off
%% Colours and styles
figure
hold on
title("Colours and styles");
plot(X1, Y1, "r", X2, Y2, "b--", X1, Y1.*(-1) , "pk");
% Add a string after X and Y to change colour and style
% The first letter is colour
% The colours are:
% blue b
% black k
% red r
% green g
% yellow y
% cyan c
% magenta m
% white w
% The next letters are for style:
% Line-types
% '-' Whole line(Default)
% '--' Dasked line
% ':' Dotted line
% '-.' Dasshed and dotted line
% Marking data-points (Just some of them)
% 'o' circle
% 'x' cross
% '*' asterisk
% 'p' star (pentagram)
legend(["Add a string after X1 and Y1 to give colour or style", "string = b--","Read comments for more"]);
hold off;
%% Automatically Save Plot
figure;
hold on
title("Automatically Save Plot");
plot(X1, Y1);
legend("A curve...");
xlabel("X");
ylabel("Y");
hold off
% TO save the current plot use:
% print( filename, "-d"+ filename )
% Use epsc instad of eps to get colours
print("Plot_filename", "-depsc");
print("Plot_filename", "-djpeg");
% string(10) makes the number 10 into "10" (Might be usefull for making filenames)
disp("Saved Plot as .jpeg and .eps ");
%% Change Window-size
% The parameters can be given when:
% making the figure(), or with the
% set()-function
f = figure('color' , 'cyan');
% The variable gcf returns the current figure
set(gcf, 'color', 'green');
% Units sets how position is calculated
% 'pixels' : (Default)
% 'normalized' : Positions on the window is represented with a
% number from 0 to 1
%'centimeters' :
%'characters' : Character-width= width of character 'x' in the
% default size
Units = 'normalized'; % Normalized is probably easiest to use
set(gcf, 'Units', Units);
% Position 0, 0 is in the bottom left corner
% Position = [ Position of left border ,
% Position of bottom border,
% width,
% height ];
Position = [0,0, 1, 1]; % ==> Full screen if 'units' = 'normalized'
set(gcf, 'Position', Position);
hold on
title("Change Window-size");
plot(X1, Y1);
legend("bg- colour set to green");
hold off
%% Misc.
% Just so the windows are propperly placed, without cluttering the code
%Get handle of all open windows
figHandles = get(groot, 'Children');
winSize = 0.5
for i = size(figHandles, 1):-1:2
x0 = 0.5-winSize/2;
y0 = 0.5-winSize/2;
Position = [x0,y0 winSize, winSize]
set(figHandles(i), 'units', 'normalized');
set(figHandles(i), 'Position', Position);
end;
uistack(figHandles(1), 'bottom'); % Put fullscreen-window behind all others