Tuesday, October 5, 2010

matlab eval 2 string linebreak to
eval2str.m

So I wanted my Matlab eval input to be made into a string I could post on a website.
The evalc() Matlab function returns a character array, but I wanted a string I could send through a socket to a website.

I had an issue with finding the exact line break, I tried char(10) and '\n' but that did not work so I cheated. Here is my cheat:

function STR2=eval2str(msg)
STR=evalc(msg) %eval input
A=evalc('X=2') %eval to get character? char(10) and \n did not work for me?
STR=strrep(STR,' ','%') %I justed used this to temporarily replace the spaces
STR2=strrep(STR,A(4),'
')
%ln2br

So if I input eval2str('X=rand(2,2)') I get a string like this:

x%=
%%%%0.4218%%%%0.7922
%%%%0.9157%%%%0.9595


Which is a string I can pass through my socket and then replace the % with ' ' on the otherside. So I can use Matlab to eval to a string output and send over a socket for a matlab server :).

No comments: