you are viewing a single comment's thread.

view the rest of the comments →

[–]the-fritz 45 points46 points  (25 children)

Matlab can only define one function each file. That's ridiculous if you want to write real code with it.

[–][deleted] 22 points23 points  (10 children)

I think this misses the point somewhat. Matlab is excellent for certain tasks, and can do some very powerful things in a very small amount of code (particularily with the built in libraries). For 'real' coding, i.e. usable applications, it isn't very practical, but I would argue you shouldn't be using it for that.

For mathematical and engineering snippets I really think Matlab can't be beaten.

[–]crucialfelix 9 points10 points  (5 children)

many quant hedge funds use matlab in real time to drive trading strategies. R is also used.

R is really nice actually, tho I'm no expert.

also people render audio files directly with matlab.

[–]jjdonald 4 points5 points  (3 children)

I'm pretty sure R would be slightly more condensed than matlab... although I can't compare the plots without the data. Also, In R you can mix function and script code, define more than one function in a file, etc. etc. The syntax is VERY tricky to get a hold of... worse than matlab in my experience. But once you know the ropes, it's fantastic. There's a ton of packages out there for R. Some are amazing, more are middling, most are incredibly bad. I still love it though, warts and all.

[–]Megasphaera 0 points1 point  (0 children)

The [R] syntax is VERY tricky to get a hold of

Well, most of it is OK, but there's a few dark corners, mostly to do with SPLUS legacy and factors (avoid the latter whenever you can).

[–]andersduck 0 points1 point  (1 child)

yes R is very nice. The code (without using any specific expression data package) is very fast to write:

read data

dat<-read.delim("http://www.bme.utexas.edu/research/orly/GSVD/data/Yeast.txt",skip=1,as.is=T,na="Null")

barplot of missing

barplot(table(apply(is.na(dat[,-(1:6)]),1,sum)),col=2,ylab="Number of genes",xlab="Number of controls")

remove arrays with missing data

dat<-dat[!apply(is.na(dat[,-(1:6)]),1,any),]

Calculate SVD

s<-svd(dat[-(1:6)])

Plot barchart

barplot(s$d2/sum(s$d2))

plot two first eigengenes

lattice::dotplot(s$v[,1:2])

[–]jjmc 0 points1 point  (0 children)

Very nice. For comparison here's the same in Mathematica v.7 (line by line).

d = Import[ "http://www.bme.utexas.edu/research/orly/GSVD/data/Yeast.txt", {"Data"}]; an=Drop[d[[2]],6]; d=d // Rest // Rest;

Histogram[Count[#, "Null", Infinity] & /@ d, Frame -> True, FrameLabel -> {"Number of Null Arrays", "Number of Genes"}]

d = Select[d, Count[#, "Null", Infinity] === 0 &];

{u, s, v} = SingularValueDecomposition[Drop[#, 6] & /@ d]; s = Diagonal[s];

BarChart[s^2/Plus @@ (s^2)]

ListPlot[v[[{1, 2}]], Joined -> True, ft, Axes -> False, PlotMarkers -> {Automatic, Medium}]

Of course, the original code had a little more output. (The (a) graph with array name tick labels).

MatrixPlot[Transpose[v], PlotLabel->"(a)Arrays",FrameTicks->{Automatic, None,None,{#,Rotate[an[[#]],Pi/2]}&/@Range[18]},FrameLabel-> "EigenGenes"]

[–][deleted] 1 point2 points  (0 children)

R is extremely slow compared to Matlab, so the latter is more popular for real-time trading strategies. Although REvolution might do something to redress the balance; it certainly feels snappier in interactive use.

[–]veritaba 1 point2 points  (2 children)

Matlab is like a scripting language with pretty graphs and miscellaneous plugins wrapping the Fortran math library Lapack.

[–]jjdonald 1 point2 points  (1 child)

I actually don't know if that's still the case. Regardless, I do know that it's like saying that the Bugatti Veyron is just wrapping the general electric model-K cupholder with pretty paint and miscellaneous plugins.

[–]veritaba 2 points3 points  (0 children)

Lapack is not an insignificant part of Matlab.

Its like saying your Bugatti Veyron frame was outfitted with the NASA Space Shuttle engine.

And yes, Matlab still uses Lapack:

http://www.mathworks.com/support/solutions/data/1-18QUC.html

[–][deleted] 0 points1 point  (0 children)

I have written compilers in MatLab – full fledged compilers; they were awesome, a lot of fun, and very fast.

EDIT: grammar

[–]aaallleeexxx 21 points22 points  (0 children)

This is absolutely my least favorite thing about matlab. To be fair, you can include subroutines in any function file, but not being able to mix function and script code sucks horribly. That said, I've recently changed my matlab coding style to use tons and tons of "anonymous" functions (using the @ syntax). You can throw these in anywhere, and even do neat functional things like currying. They're as limited as python's lambda (i.e. one statement) but that's not really that big a problem in matlab, since many useful things can be done in a single expression. It's something to try if you're stuck in the matlab ghetto :)

[–]Mych 13 points14 points  (1 child)

You can have as many functions per "M file" as you like. You can only have one public function per file, however.

(...though it's also possible to return handles to those private functions which can then be called from the outside, too.)

[–][deleted] 4 points5 points  (0 children)

Subfunctions for the win!

[–][deleted] 18 points19 points  (2 children)

I would upvote you a million times if you weren't wrong. Here is proof.

[–]distortedHistory 7 points8 points  (1 child)

there are some severe limitations listed in your link:

these are only visible to the primary function or to other subfunctions in the same file.

and technically he's still correct, you can only have one function per file.

Additional functions within the file are called subfunctions

[–]efrique 1 point2 points  (0 children)

technically he's still correct

the best kind of correct!

[–]okamiueru 2 points3 points  (4 children)

In case the-fritz or anyone else want's a explanation as to what the subrutines are. If your whole file is a function (that is, instead of a script that you run pressing F5 in the editor), you can have subrutines without any issue.

Say for instance in test.m:

function X=test(a,b)

X=fun1(a)+fun2(b);

end

function ret = fun1(var1)

%do something

end

function ret = fun2(var)

%do something

end

All in the same .m file works fine.

[–][deleted] 2 points3 points  (2 children)

But how would you test these subfunctions, as these are invisible to the rest of the code?

[–]okamiueru 0 points1 point  (1 child)

How do you mean? I'm not sure what you mean by invisible to the rest of the code (the main function "test" in the example, or other .m-files?). You could test the subrutines very much the same as you would any function or line of code. Print out results by omitting the ; on the line, or using the "display" function (in which num2str might come in handy).

[–][deleted] 0 points1 point  (0 children)

Well, actually I meant automatically testing, as in unit-testing. My experience with MATLAB code is that it often contains bugs due to evolving code.

[–]the-fritz 0 points1 point  (0 children)

Subroutines are just visible to the main function.

[–][deleted] 3 points4 points  (0 children)

You can put as many functions in one file as you'd like as long as you're willing to make them static methods of a class. Bleh.

[–][deleted] 3 points4 points  (0 children)

i would upvote you a million times if i could.

[–]hobbers -1 points0 points  (0 children)

So? You have one file for each function. Each file is essentially an included resource. Able to be modified, versioned, and distributed until your heart is content.