data = struct('x_data', 2, 'y_data', 3)
data.x_data
trial1 = struct('Name', 'Trial1_23213', 'Time', '2019/01/22', 'Data', struct('x', 2, 'y', 3))
trial1.Data.x = 17
trial1.Name = 'Test'
data = struct() % declare empty, add fields later
data.x = 0:10 % add field dynamically
data.y = sin(data.x) % add another field dynamically
data
trials = struct( ...
'Name', {'Trial1', 'Trial2', 'Trial3'}, ...
'XData', {2, 3, 4}, ...
'YData', {2, 5, 7})
trials(1) % treat like a vector of structures
Just form a cell array from structs.
trial1 = trials(1);
trial2 = trials(2);
trial3 = trials(3);
trials2 = {trial1, trial2, trial3} % treat like a cell array of structures
trials2{1} % access like a cell array
single plot
x = 1:10;
y = sin(x);
figure(1); % not necessary, but recommended, be explicit, create figure
plot(x, y); % plots live in a figure
multiple plots in same figure
x = linspace(0, 10, 100);
y1 = sin(x);
y2 = cos(x);
% by default, plots get overwrittten
figure(2);
plot(x, y1);
hold on; % use after first plot-like command
plot(x, y2);
title, axis label, axis range
x = linspace(-5, 5, 100);
%y = sin(x) ./ x;
%or
y = zeros(1, length(x));
for i = 1:length(x)
y(i) = sin(x(i)) / x(i);
end
figure(3);
plot(x, y);
title('My Plot');
xlabel('Time, t (s)');
ylabel('Values (1)');
xlim([-5, 5]);
ylim([-1, 1]);
plot markers
figure(5);
%plot(x, y, 'rx'); % shorthand notation
% full notation
plot(x, y, 'LineStyle', 'none', 'MarkerEdgeColor', 'red', 'Marker', 'x');
% scatter(x, y) is plot(x, y, 'o')
x = linspace(-5, 5, 100);
y = sin(x) ./ x;
% properties can be set via set function
figure(3);
p = plot(x, y);
set(p, 'DisplayName', 'Experiment Data'); % plot label
set(p, 'Marker', 'o');
hold on; % recommended after the first plot in a series
% or via property pairs (name, value) in the call to the function
plot(x, cos(x), 'DisplayName', 'Model Prediction');
plot(x, x.^2 / 16, 'HandleVisibility', 'off'); % to not show a label
% title with a property set
title('My Plot', 'FontSize', 25);
% everything has properties
xl = xlabel('Time, t (s)');
set(xl, 'FontSize', 25);
ylabel('Values (1)', 'FontSize', 25);
% limit the axis range
xlim([-5, 5]);
ylim([-1, 1]);
% don't forget to show labels of plots
legend('show', 'location', 'south');
%legend('Experiment', 'Model'); % alternatively, discouraged
logarithmic axis
x = linspace(0, 10, 100);
y = log(x);
figure(6);
p = plot(x, y);
set(gca, 'XScale', 'log');
% or
% semilogx(x, y)
% create a grid
grid("on");
grid("minor");
subplots
figure(); % request a new figure
subplot(2, 1, 1);
plot(x, sin(x));
subplot(2, 1, 2);
plot(x, cos(x));
figure();
subplot(2, 1, 1);
plot(x, sin(x));
subplot(2, 2, 3);
plot(x, cos(x));
subplot(2, 2, 4);
plot(x, -cos(x));
histogram
r = rand(1000, 1); % uniform distribution
hist(r, 20); % hist(variable, nb_of_bins)
r = randn(1000, 1); % normal (Gaussian) distribution, notice randN
hist(r, 20); % hist(variable, nb_of_bins)
% Meshgrid creates pairs for each x and y
x = linspace(-10.0, 1.0, 5)
y = linspace(-2.0, 2.0, 3)
[X, Y] = meshgrid(x, y)
f = @(x, y) x.^2 - y.^2;
N = 30;
x = linspace(-2.0, 2.0, N);
y = linspace(-2.0, 2.0, N);
[X, Y] = meshgrid(x, y);
Z = f(X, Y);
figure(1);
surf(X, Y, Z);
figure(2);
contour(X, Y, Z, 20);
figure(3);
contourf(X, Y, Z, 20);
colorbar; % range of Z