Skip to content

Commit b4d4f90

Browse files
committed
examples from MATLAB Expo 2022 presentation
1 parent ec02f24 commit b4d4f90

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# bridge_numpy_index.py
2+
def ind(z, row, col):
3+
return z[int(row)-1, int(col)-1]
4+
def nind(z, *i):
5+
int_minus_1 = tuple([int(_) - 1 for _ in i])
6+
return z[int_minus_1]

matlab_expo_2022/demo_openpyxl.m

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
% Al Danial, David Garrison
2+
Im = @py.importlib.import_module;
3+
OP = Im("openpyxl");
4+
styles = Im("openpyxl.styles");
5+
Font = styles.Font;
6+
Alignment = styles.Alignment;
7+
PatternFill = styles.PatternFill;
8+
book = OP.Workbook();
9+
sheet = book.active;
10+
sheet.title = "Pets by weight";
11+
12+
% font styles, background color
13+
ft_title = Font(...
14+
pyargs("name","Arial", ...
15+
"size",int64(14),"bold",py.True));
16+
ft_red = Font(color="00FF0000");
17+
ft_italics = Font(bold=py.True,...
18+
italic=py.True);
19+
bg_green = PatternFill( ...
20+
fgColor="C5FD2F", fill_type="solid");
21+
22+
sheet.merge_cells("B2:D3");
23+
B2 = sheet.cell(2,2);
24+
B2.value = "My Pets";
25+
B2.font = ft_title;
26+
B2.alignment = Alignment(...
27+
horizontal="center", vertical="center");
28+
29+
% column headings
30+
category={"Name","Animal","weight [kg]"};
31+
row = int64(4); col = int64(1);
32+
for i = 1:length(category)
33+
nextCell = sheet.cell(row, col+i);
34+
nextCell.value = category{i};
35+
nextCell.fill = bg_green;
36+
end
37+
38+
pets = {{"Nutmeg", "Rabbit", 2.5},...
39+
{"Annabel", "Dog", 4.3}, ...
40+
{"Sunny", "Bird", 0.02}, ...
41+
{"Harley", "Dog", 17.1}, ...
42+
{"Toby", "Dog", 24.0}, ...
43+
{"Mr Socks", "Cat", 3.9}};
44+
45+
for P = pets
46+
row = row + 1;
47+
for j = 1:length(category)
48+
nextCell = cell(sheet,row,col+j);
49+
nextCell.value = P{1}{j};
50+
if j == 3 && P{1}{j} < 0.1
51+
nextCell = cell(sheet,row,col+j);
52+
nextCell.font = ft_red;
53+
end
54+
end
55+
end
56+
57+
% equation to sum all weights
58+
eqn = sprintf("=SUM(D4:D%d)", row);
59+
nextCell = sheet.cell(row+1, 4);
60+
nextCell.value = eqn;
61+
62+
nextCell = sheet.cell(row+1, 2);
63+
nextCell.value = "Total weight:";
64+
nextCell.font = ft_italics;
65+
66+
book.save("pets.xlsx")

matlab_expo_2022/demo_openpyxl.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
3+
import openpyxl as OP
4+
import openpyxl.styles as styles
5+
Font = styles.Font
6+
Alignment = styles.Alignment
7+
PatternFill = styles.PatternFill
8+
book = OP.Workbook()
9+
sheet = book.active
10+
sheet.title = "Pets by weight"
11+
12+
# font styles, background color
13+
ft_title = Font(name="Arial",
14+
size=14,
15+
bold=True)
16+
ft_red = Font(color="00FF0000")
17+
ft_italics = Font(bold=True,
18+
italic=True)
19+
bg_green = PatternFill(
20+
fgColor="C5FD2F",fill_type="solid")
21+
22+
sheet.merge_cells("B2:D3")
23+
B2 = sheet.cell(2,2)
24+
B2.value = "My Pets"
25+
B2.font = ft_title
26+
B2.alignment = Alignment(
27+
horizontal="center",vertical="center")
28+
29+
# column headings
30+
category=["Name","Animal","weight [kg]"]
31+
row, col = 4, 2
32+
for i in range(len(category)):
33+
nextCell = sheet.cell(row, col+i)
34+
nextCell.value = category[i]
35+
nextCell.fill = bg_green
36+
37+
38+
pets = [["Nutmeg", "Rabbit", 2.5],
39+
["Annabel", "Dog", 4.3],
40+
["Sunny", "Bird", 0.02],
41+
["Harley", "Dog", 17.1],
42+
["Toby", "Dog", 24.0],
43+
["Mr Socks", "Cat", 3.9]]
44+
45+
for P in pets:
46+
row += 1
47+
for j in range(len(category)):
48+
cell = sheet.cell(row,col+j,
49+
P[j])
50+
if j == 2 and P[j] < 0.1:
51+
nextCell = sheet.cell(row,col+j)
52+
nextCell.font = ft_red
53+
54+
55+
56+
57+
# equation to sum all weights
58+
eqn = f"=SUM(D4:{row+1})"
59+
nextCell = sheet.cell(row+1, 4)
60+
nextCell.value = eqn
61+
62+
nextCell = sheet.cell(row+1, 2)
63+
nextCell.value = "Total weight:"
64+
nextCell.font = ft_italics;
65+
66+
book.save("pets.xlsx")

matlab_expo_2022/imp.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function [module] = imp(module_name)
2+
% Abbreviaiton for py.importlib.import_module(module_name)
3+
module = py.importlib.import_module(module_name)
4+
end

matlab_expo_2022/optim_config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# optim_config.yaml
2+
max_iter : 1000
3+
newmark :
4+
alpha : 0.25
5+
beta : 0.5
6+
input_dir : "/xfer/sim_data/2022/05/17"
7+
tune_coeff : [1.2e-4, -3.25, 58.2]

matlab_expo_2022/pets.xlsx

5.1 KB
Binary file not shown.

matlab_expo_2022/txy.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# txy.py
2+
import numpy as np
3+
from datetime import datetime
4+
def F():
5+
return { 't' : datetime.now(),
6+
'x' : np.arange(12).reshape(2,6),
7+
'y' : ['a list', 'with strings']}

0 commit comments

Comments
 (0)