MatLab string demo

Contents

Creating strings.

Anything between two single quotes is a string.

a = 'this'
a =

this

a blank string

b = ' '
b =

 

a simple verb

c = 'is'
c =

is

include apostrophe:

d = 'ain''t'
d =

ain't

Working with strings

Assigning strings

x=a
x =

this

Joining strings

x = [a b c b a]
x =

this is this

Can also combine variables with strings

x = [a b c b a '!  ' a b d ' something else.']
x =

this is this!  this ain't something else.

displaying a string

disp(x)
this is this!  this ain't something else.

Combining numbers with strings

num2str changes number to string

ch=1;  pw=900;  T=300;

num2str(pw)
ans =

900

Make a command for the I/O board

cmd = ['#' num2str(ch) 'P' num2str(pw) 'T' num2str(300)]
cmd =

#1P900T300

What happens if we calculate pulse width and its not integer

pw=900.35
pw =

  900.3500

Make a command for the I/O board

cmd = ['#' num2str(ch) 'P' num2str(pw) 'T' num2str(300)]
cmd =

#1P900.35T300

This is no good, so round the number

round(pw)
ans =

   900

Now we can make a clean command for the I/O board

cmd = ['#' num2str(ch) 'P' num2str(round(pw)) 'T' num2str(300)]
cmd =

#1P900T300

So if we had a serial port connection 's,' we could send it thusly:

"printf(s,cmd)"

Plotting data in MatLab

x=[1 2.1 2.9 4];
y=[10 8.1 5.9 3.8];

simple plot

plot(x,y)
xlabel('X values');
ylabel('Y values');
title('Simple plot');

change color, add marker, change linestyle

plot(x,y,'r:o');