Thread Subject: How far is Matlab related to C?

Subject: How far is Matlab related to C?

From: Michael Bell

Date: 26 Jan, 2008 18:25:55

Message: 1 of 5

I am considering getting involved in a project which will run in
Matlab. I have some knowledge of C. How useful will my knowledge of C
be to me?

Michael Bell

--

Subject: How far is Matlab related to C?

From: James Tursa

Date: 26 Jan, 2008 18:53:49

Message: 2 of 5

On Sat, 26 Jan 2008 18:25:55 GMT, Michael Bell
<michael@beaverbell.co.uk> wrote:

>I am considering getting involved in a project which will run in
>Matlab. I have some knowledge of C. How useful will my knowledge of C
>be to me?
>
>Michael Bell

Having knowedge of *some* programming language will help. But having
knowedge of C specifically will not help that much. One similarity to
C is you can think of MATLAB as a "pass-by-value" language for
function calls (actually a "lazy-copy" method). But the concepts of
vectorized processing and vectorized function calls will probably be
new to you, and this is quite important in MATLAB programming. Fortran
is actually a much better match to MATLAB than C is in this respect.
MATLAB does have the ability to interface with C via what is call a
mex or engine application. If you do this then C knowledge will come
in very handy.

James Tursa

Subject: How far is Matlab related to C?

From: dpb

Date: 26 Jan, 2008 19:01:00

Message: 3 of 5

James Tursa wrote:
> On Sat, 26 Jan 2008 18:25:55 GMT, Michael Bell
> <michael@beaverbell.co.uk> wrote:
>
>> I am considering getting involved in a project which will run in
>> Matlab. I have some knowledge of C. How useful will my knowledge of C
>> be to me?
>>
>> Michael Bell
>
> Having knowedge of *some* programming language will help. But having
> knowedge of C specifically will not help that much. One similarity to
> C is you can think of MATLAB as a "pass-by-value" language for
> function calls (actually a "lazy-copy" method).

It really isn't by-value at all--any function argument can be modified.
  It's been a while since I looked at the details, but isn't more "copy
in, copy out"?


...

> ...Fortran
> is actually a much better match to MATLAB than C is in this respect.

I'd say overall, it's a much better match than C in almost all
respects--syntax is far more Fortran-like than C (other than the
arguments to the xprintf() functions (which, parenthetically I detest as
compared to a Fortran FORMAT equivalent :) ) I can't think of much that
is particularly "C-like")... :)

--

Subject: How far is Matlab related to C?

From: James Tursa

Date: 26 Jan, 2008 20:53:34

Message: 4 of 5

On Sat, 26 Jan 2008 13:01:00 -0600, dpb <none@non.net> wrote:

>
>It really isn't by-value at all--any function argument can be modified.
> It's been a while since I looked at the details, but isn't more "copy
>in, copy out"?
>

No. I disagree and stand by my original statement. MATLAB *is*
effectively pass-by-value very similar in concept to C and different
from Fortran which is pass-by-reference. In MATLAB, the function
arguments inside a function *can* be modified, but they don't change
the original. Try it. This is similar to any non-const argument in C
being modified inside a function, without changing the original
argument used in the call. For instance:

>> b=8
b =
     8
>> funtest(b)
ans =
     5
>> b
b =
     8
>>

function a = funtest(b)
b = 7;
a = 5;
return
end

Changing b inside funtest did *not* change the original b in the
calling statement. This is effectively pass-by-value (and I already
pointed out that MATLAB actually uses a lazy-copy scheme). The only
caveat I would add is that this is NOT true of mex function calls,
where the scheme becomes pass-by-reference and the input arguments
should *never* be modified inside a mex function.

James Tursa

Subject: How far is Matlab related to C?

From: dpb

Date: 26 Jan, 2008 21:46:54

Message: 5 of 5

James Tursa wrote:
> On Sat, 26 Jan 2008 13:01:00 -0600, dpb <none@non.net> wrote:
>
>> It really isn't by-value at all--any function argument can be modified.
>> It's been a while since I looked at the details, but isn't more "copy
>> in, copy out"?
>>
>
> No. I disagree and stand by my original statement. MATLAB *is*
> effectively pass-by-value very similar in concept to C and different
> from Fortran which is pass-by-reference. ...

You're right; don't know what happened to me at the time... :(

Fortran, of course, in the Standard doesn't mention the mechanism; only
what the effect is -- that it "looks like" pass-by-reference and is
often implemented in that fashion is a result, but there's nothing that
actually requires it. (It is a constant reminder on c.l.f of the
difference between what an implementation may be as opposed to the
Standard).

--

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com