Skip to content
Jack edited this page Feb 12, 2017 · 1 revision

Common Tasks

Some common tasks in MATLAB made better by sxm:

Plot this function over this domain and save it as a PNG

Most first year calculus and linear algebra courses really love this problem. It's straightforward in vanilla MATLAB:

X = 0:0.1:2;
Y = sin(X);
plot(S, Y, 'r.');
print('-dpng', 'my_plot.png');

This has the disadvantage of polluting the workspace and makes it awkward to do this for several functions (as one might expect in a typical session). With sxm, we can store each plot conveniently:

myplot = sxm_Plot(0:0.1:2, sin(0:0.1:2), [], 'r.');
myplot.plot(); % generate plot
myplot.save('my_plot.png'); % save it to disk

Determining membership of a vector

This one sounds very simple but vanilla MATLAB takes an interesting approach. MATLAB gives us the familiar == equality operator, but it works interestingly for vectors and single elements:

vec = [1 2 3 4 5];
res = (vec == 2); % res = [0 1 0 0 0]

This is fine for visual inspection, but seems contrived programmatically. MATLAB gives us the any function, which we can use to remedy this:

actual_answer = any(res); % actual_answer = 1

In sxm, these two functions are simply wrapped by sxm_in:

vec = [1 2 3 4 5]
actual_answer = sxm_in(2, vec); % actual_answer = 2