Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
matlab / fortran equivalent statements (or similar) help

Subject: matlab / fortran equivalent statements (or similar) help

From: Marina

Date: 20 Dec, 2009 00:37:12

Message: 1 of 6

Hello everyone,

please, I need your help.

I've just recently started using matlab for my thesis project, and so
far I like it. The only thing I cannot understand is the equivalent of
fortran's format statement (I know fortran very well, but due to some
constaints by my mentor I have to use matlab. Not saying that's a bad
thing (not at all); just answering the obvious "Why not continue using
fortran then?" questions).

Would anyone here be fluent in both of them ? If so, would it be much
trouble to explain to me how in matlab to accomplish, let's say,
what's done by

format(*,'("This is a string",3F8.3,2I3.3,"Another string")')x,y,z,i,j

where the aforementioned are defined earlier.

Marina

Subject: matlab / fortran equivalent statements (or similar) help

From: ImageAnalyst

Date: 20 Dec, 2009 00:52:36

Message: 2 of 6

MATLAB is more like the C language. Hence you'll find the sprintf()
function instead of format(). It's described in the help.
format(*,'("This is a string",3F8.3,2I3.3,"Another string")')
x,y,z,i,j
might become

% Make up some values.
x=10,y=2.8888,z=3.333
i=4,j=8

% Stuff them into a string using the sprintf() function.
% The string will print into the command line because I
% didn't put a semicolon after it. Semicolon would suppress display.
outputString = sprintf('This is a string %8.3f, %8.3f, %8.3f, %3d,
%3d. Another string', x, y, z, i, j)

Subject: matlab / fortran equivalent statements (or similar) help

From: Marina

Date: 20 Dec, 2009 02:43:16

Message: 3 of 6

On Sat, 19 Dec 2009 16:52:36 -0800 (PST), ImageAnalyst
<imageanalyst@mailinator.com> wrote:

>MATLAB is more like the C language. Hence you'll find the sprintf()
>function instead of format(). It's described in the help.
>format(*,'("This is a string",3F8.3,2I3.3,"Another string")')
>x,y,z,i,j
>might become
>
>% Make up some values.
>x=10,y=2.8888,z=3.333
>i=4,j=8
>
>% Stuff them into a string using the sprintf() function.
>% The string will print into the command line because I
>% didn't put a semicolon after it. Semicolon would suppress display.
>outputString = sprintf('This is a string %8.3f, %8.3f, %8.3f, %3d,
>%3d. Another string', x, y, z, i, j)

Hi ImageAnalyst,

thanks for the quick reply. Really appreciate it.

I stumbled onto sprintf function, but was hoping, since I read matlab
was made onto fortran foundations (so to speak), it would have
something more similar to (rather powerful, you must admit) format
statement.

In any case, this will do for now, I gather. Just to be on the safe
side though ... are the numbers equivalent to fortran's format ones.
It looks like it from the example, but is there something I should be
aware of when making code translation (e.g. can I just rewrite formats
in this manner ?)

In any case, thanks again,
and best wishes for the upcoming holidays !
Marina

Subject: matlab / fortran equivalent statements (or similar) help

From: ImageAnalyst

Date: 20 Dec, 2009 05:00:49

Message: 4 of 6

It's been more than 20 years since I used fortran so I can't help
you. I don't know why you have both single and double quotes,
asterisks, etc. and I don't think you can triple up on a %f like you
did in Fortran - you need to have a separate %f for each number, or
else just a single one but then combine several sprintfs if you want
to append single numbers after that. Bottom line, you're going to
have to write and debug each sprintf one at a time. Another quirk is
that you can't continue a literal string with ... like you can with
most lines. If you're in a string, it's going to have to fit all on
one line, or else use multiple sprintfs like this
message = sprintf('This is line #1 with value %d', value1);
% Append more stuff to the string in a new line.
message = sprintf('%s\nThis is line #2 with value %d', message,
value2);

Subject: matlab / fortran equivalent statements (or similar) help

From: James Tursa

Date: 21 Dec, 2009 00:18:02

Message: 5 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2f69b5e2-61b0-4273-8fad-d038dbc5e346@e27g2000yqd.googlegroups.com>...
> It's been more than 20 years since I used fortran so I can't help
> you. I don't know why you have both single and double quotes,

FYI, Fortran allows either single quotes or double quotes to delineate strings. So if you want a quoted string inside a string literal, it saves from having to double-up the quotes. e.g., these are all valid Fortran string literals, but those with quotes inside strings need to double-up unless the containing quotes are different:

"This is a string"
'This is a string'

'A string with a single quote '' inside it'
"A string with a single quote ' inside it"

"A string with a double quote "" inside it"
'A string with a double quote " inside it'

Each pair above represents exactly the same string literal.

James Tursa

Subject: matlab / fortran equivalent statements (or similar) help

From: Steven Lord

Date: 1 Jan, 2010 04:10:44

Message: 6 of 6


"ImageAnalyst" <imageanalyst@mailinator.com> wrote in message
news:2f69b5e2-61b0-4273-8fad-d038dbc5e346@e27g2000yqd.googlegroups.com...

*snip*

> Another quirk is
> that you can't continue a literal string with ... like you can with
> most lines. If you're in a string, it's going to have to fit all on
> one line, or else use multiple sprintfs like this
> message = sprintf('This is line #1 with value %d', value1);
> % Append more stuff to the string in a new line.
> message = sprintf('%s\nThis is line #2 with value %d', message,
> value2);

You can continue a literal string with ... as long as you do it as part of a
concatenation.

x = 1;
y = 2;
message = sprintf(['The value of x is %d and ', ...
    'the value of y is %d.\n'], x, y)

% or
message2 = sprintf(['The value of x is %d.\n', ...
    'The value of y is %d.\n'], x, y)

Looking at the OP:

> > format(*,'("This is a string",3F8.3,2I3.3,"Another string")')x,y,z,i,j

I don't have a lot of experience with Fortran (more reading than writing)
but if I interpret this correctly, the equivalent of this in MATLAB would be
along the lines of:

x = 1.1;
y = 2.2;
z = 3.3;
i = 4;
j = 5;
msg = sprintf(['(This is a string', ...
    repmat('%8.3f ', 1, 3), ...
    repmat('%3.3i ', 1, 2), ...
    'Another string)'], ...
    x, y, z, i, j)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

No tags are associated with this thread.

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.

Contact us