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:
fprintf is not reading the \n

Subject: fprintf is not reading the \n

From: Ellen

Date: 29 Jun, 2012 18:41:18

Message: 1 of 29

I need to have a matrix output to a file, I have it as a variable called 'First' and it's 7480x2, pretty big. The code I have been trying to use is:

fid=fopen('data_file.txt','w');
fprintf(fid,'%d','%d \n',First);
fclose(fid);

It is just printing it as one long string of numbers. Also I'm on a mac, so requiring the \r or wt shouldn't be an issue, but I tried them as well. I used the save function before, but I didn't like how it made all the data into exponential form because it is all just numbers between 1-10 000.

Someone was suggesting something about how it needs to read and print the data line by line, however I don't know how to do that.

Any help or insight into this is much appreciated. :)

Subject: fprintf is not reading the \n

From: dpb

Date: 29 Jun, 2012 18:51:19

Message: 2 of 29

On 6/29/2012 1:41 PM, Ellen wrote:
> I need to have a matrix output to a file, I have it as a variable called
> 'First' and it's 7480x2, pretty big. The code I have been trying to use is:
>
> fid=fopen('data_file.txt','w');
> fprintf(fid,'%d','%d \n',First);
> fclose(fid);
>
...

Bad format string. You've got two string fields separated by a comma
instead of a single string. You don't say whether you want
comma-separated or not...

fprintf(fid,'%d, %d\n',First'); % Assuming comma-delimited

fprintf(fid,'%d %d\n',First'); % No comma

NB the ' transpose. Try a small test array to see/understand
column-major order and why it's needed.

If you do want a delimited file check out

doc dlmwrite

--

Subject: fprintf is not reading the \n

From: anyone

Date: 29 Jun, 2012 19:08:14

Message: 3 of 29

On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:

> I need to have a matrix output to a file, I have it as a variable called
> 'First' and it's 7480x2, pretty big. The code I have been trying to use
> is:
>
> fid=fopen('data_file.txt','w');
> fprintf(fid,'%d','%d \n',First);
> fclose(fid);
>
> It is just printing it as one long string of numbers.

Naturally, since that is what you asked Matlab to do.

[...]
> Someone was suggesting something about how it needs to read and print
> the data line by line, however I don't know how to do that.

Try something like this:
 
fid=fopen('data_file.txt','wt'); % open a target file
for i = 1:7480 % loop on reading data
     fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
end
fclose(fid); % close target file

> Any help or insight into this is much appreciated. :)

adjust "%14.10e" (and so on) as needed for appropriate field width

Subject: fprintf is not reading the \n -- correction

From: anyone

Date: 29 Jun, 2012 19:21:05

Message: 4 of 29

On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:

(managed to drop a carriage return in earlier post)

[...]
> Someone was suggesting something about how it needs to read and print
> the data line by line, however I don't know how to do that.

Try something like this:
 
fid=fopen('data_file.txt','wt'); % open a target file

for i = 1:7480
% loop on reading data
     fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
end fclose(fid); % close target file

> Any help or insight into this is much appreciated. :)

adjust "%14.10e" (and so on) as needed for appropriate field width

Subject: fprintf is not reading the \n -- arrgh...

From: anyone

Date: 29 Jun, 2012 19:28:22

Message: 5 of 29

On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:

what I need is a 'do what I mean' computer...

[...]
> Someone was suggesting something about how it needs to read and print
> the data line by line, however I don't know how to do that.

 
fid=fopen('data_file.txt','wt'); % open a target file

for i = 1:7480 % loop on reading data
     fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
end

fclose(fid); % close target file

Subject: fprintf is not reading the \n

From: dpb

Date: 29 Jun, 2012 19:41:23

Message: 6 of 29

On 6/29/2012 2:08 PM, anyone wrote:
> On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:
>> I need to have a matrix output to a file, ...
...
>> fprintf(fid,'%d','%d \n',First);
...
>> It is just printing it as one long string of numbers.
>
> Naturally, since that is what you asked Matlab to do.

That's kinda' snarky... :(
> [...]
>> Someone was suggesting something about how it needs to read and print
>> the data line by line, however I don't know how to do that.
>
> Try something like this:
...
> for i = 1:7480
> fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
...

Not "_the_Matlab_way_" -- no loop needed. Use ' operator. See other
response.

--

Subject: fprintf is not reading the \n

From: Ellen

Date: 29 Jun, 2012 20:22:13

Message: 7 of 29

dpb <none@non.net> wrote in message <jsl0d1$s5h$1@speranza.aioe.org>...
> On 6/29/2012 2:08 PM, anyone wrote:
> > On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:
> >> I need to have a matrix output to a file, ...
> ...
> >> fprintf(fid,'%d','%d \n',First);
> ...
> >> It is just printing it as one long string of numbers.
> >
> > Naturally, since that is what you asked Matlab to do.
>
> That's kinda' snarky... :(
> > [...]
> >> Someone was suggesting something about how it needs to read and print
> >> the data line by line, however I don't know how to do that.
> >
> > Try something like this:
> ...
> > for i = 1:7480
> > fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
> ...
>
> Not "_the_Matlab_way_" -- no loop needed. Use ' operator. See other
> response.
>
> --

Thank you!! Both ways worked regardless if they are correct or not, but the ' operator is definitely a lot simpler.

Subject: fprintf is not reading the \n

From: dpb

Date: 29 Jun, 2012 20:58:04

Message: 8 of 29

On 6/29/2012 3:22 PM, Ellen wrote:
...

> Thank you!! Both ways worked regardless if they are correct or not, but
> the ' operator is definitely a lot simpler.

It's not that the other was is really "incorrect"; it's simply more
concise and generally faster as well so it should be preferred as a
stylistic choice unless there's some real reason for needing the loop
otherwise.

--

Subject: fprintf is not reading the \n

From: dpb

Date: 30 Jun, 2012 00:10:12

Message: 9 of 29

On 6/29/2012 2:41 PM, dpb wrote:
> On 6/29/2012 2:08 PM, anyone wrote:
>> On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:
>>> I need to have a matrix output to a file, ...
> ....
>>> fprintf(fid,'%d','%d \n',First);
> ....
>>> It is just printing it as one long string of numbers.
>>
>> Naturally, since that is what you asked Matlab to do.
>
> That's kinda' snarky... :(
>> [...]
>>> Someone was suggesting something about how it needs to read and print
>>> the data line by line, however I don't know how to do that.
>>
>> Try something like this:
> ....
>> for i = 1:7480
>> fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
> ....
>
> Not "_the_Matlab_way_" -- no loop needed. Use ' operator. See other
> response.

BTW, just a couple stylistic comments...

'i' in Matlab is the imaginary i, sqrt(-1). Aliasing it w/ a variable
can lead to confusion so is better to use another variable besides i
(and j, same reason) for loop indices despite the propensity to do so in
other languages.

If one were to write the loop then at least do something like

fprintf(fid,fmt,x(i,:)

rather than writing each element individually.

--

Subject: fprintf is not reading the \n

From: anyone

Date: 30 Jun, 2012 13:15:05

Message: 10 of 29

On Fri, 29 Jun 2012 14:41:23 -0500, dpb wrote:

> On 6/29/2012 2:08 PM, anyone wrote:
>> On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:
>>> I need to have a matrix output to a file, ...
> ...
>>> fprintf(fid,'%d','%d \n',First);
> ...
>>> It is just printing it as one long string of numbers.
>>
>> Naturally, since that is what you asked Matlab to do.
>
> That's kinda' snarky... :(
>> [...]
>>> Someone was suggesting something about how it needs to read and print
>>> the data line by line, however I don't know how to do that.
>>
>> Try something like this:
> ...
>> for i = 1:7480
>> fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
> ...
>
> Not "_the_Matlab_way_" -- no loop needed. Use ' operator. See other
> response.

"...something like..." does not imply 'the only way', does it?

Subject: fprintf is not reading the \n

From: anyone

Date: 30 Jun, 2012 13:16:45

Message: 11 of 29

On Fri, 29 Jun 2012 19:10:12 -0500, dpb wrote:

> On 6/29/2012 2:41 PM, dpb wrote:
>> On 6/29/2012 2:08 PM, anyone wrote:
>>> On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:
>>>> I need to have a matrix output to a file, ...
>> ....
>>>> fprintf(fid,'%d','%d \n',First);
>> ....
>>>> It is just printing it as one long string of numbers.
>>>
>>> Naturally, since that is what you asked Matlab to do.
>>
>> That's kinda' snarky... :(
>>> [...]
>>>> Someone was suggesting something about how it needs to read and print
>>>> the data line by line, however I don't know how to do that.
>>>
>>> Try something like this:
>> ....
>>> for i = 1:7480 fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
>> ....
>>
>> Not "_the_Matlab_way_" -- no loop needed. Use ' operator. See other
>> response.
>
> BTW, just a couple stylistic comments...
>
> 'i' in Matlab is the imaginary i,

So is 'j'. Those who understand what they're doing aren't terribly
disturbed by the choice.

Subject: fprintf is not reading the \n

From: Doug Schwarz

Date: 30 Jun, 2012 17:57:44

Message: 12 of 29

In article <4feefc3d$0$45605$c3e8da3$aae71a0a@news.astraweb.com>,
 anyone <ieGJnr4aQXRbuzEL@xwkjXHz0Acndafrl63gnXqgtI4QbMxeC.dna> wrote:

> On Fri, 29 Jun 2012 19:10:12 -0500, dpb wrote:
>
> > On 6/29/2012 2:41 PM, dpb wrote:
> >> On 6/29/2012 2:08 PM, anyone wrote:
> >>> On Fri, 29 Jun 2012 18:41:18 +0000, Ellen wrote:
> >>>> I need to have a matrix output to a file, ...
> >> ....
> >>>> fprintf(fid,'%d','%d \n',First);
> >> ....
> >>>> It is just printing it as one long string of numbers.
> >>>
> >>> Naturally, since that is what you asked Matlab to do.
> >>
> >> That's kinda' snarky... :(
> >>> [...]
> >>>> Someone was suggesting something about how it needs to read and print
> >>>> the data line by line, however I don't know how to do that.
> >>>
> >>> Try something like this:
> >> ....
> >>> for i = 1:7480 fprintf(fid,'%14.10e %5.2f\n',First(i,1),First(i,2));
> >> ....
> >>
> >> Not "_the_Matlab_way_" -- no loop needed. Use ' operator. See other
> >> response.
> >
> > BTW, just a couple stylistic comments...
> >
> > 'i' in Matlab is the imaginary i,
>
> So is 'j'. Those who understand what they're doing aren't terribly
> disturbed by the choice.

Also, dpb, you have mentioned that you use an old version of MATLAB.
Newer versions can use 1i or 1j for the imaginary unit (not sure how far
back that goes). I use i and j all the time as variables and use 1i or
sqrt(-1) for the imaginary unit. It has never confused anyone.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: fprintf is not reading the \n

From: dpb

Date: 30 Jun, 2012 18:01:32

Message: 13 of 29

On 6/30/2012 8:15 AM, anyone wrote:
...

> "...something like..." does not imply 'the only way', does it?

No, but it doesn't lead down the path of righteousness, either... :)

Why give a newbie that kind of guidance as first choice?

--

Subject: fprintf is not reading the \n

From: dpb

Date: 30 Jun, 2012 18:14:25

Message: 14 of 29

On 6/30/2012 12:57 PM, Doug Schwarz wrote:
...

> Also, dpb, you have mentioned that you use an old version of MATLAB.
> Newer versions can use 1i or 1j for the imaginary unit (not sure how
> far back that goes). I use i and j all the time as variables and use
> 1i or sqrt(-1) for the imaginary unit. It has never confused
> anyone.

As long as are careful yes one can get by--the parser even in this
version is smart enough to recognize the imaginary i/j in context _IF_
the leading digit is written since it would otherwise be a syntax error
to follow a constant w/ a variable name w/o an intervening operator--but
who routinely writes 1i or 1j in place of just i or j (at least w/o
having been warned)? I think it still worth making the point...

I don't know if TMW has added the clarification to the documentation or
not; it's not in this release, anyway.

--

Subject: fprintf is not reading the \n

From: dpb

Date: 30 Jun, 2012 18:28:06

Message: 15 of 29

On 6/30/2012 1:14 PM, dpb wrote:
> On 6/30/2012 12:57 PM, Doug Schwarz wrote:
> ....
>
>> Also, dpb, you have mentioned that you use an old version of MATLAB.
>> Newer versions can use 1i or 1j for the imaginary unit (not sure how
>> far back that goes). I use i and j all the time as variables and use
>> 1i or sqrt(-1) for the imaginary unit. It has never confused
>> anyone.
>
> As long as are careful yes one can get by--the parser even in this
> version is smart enough to recognize the imaginary i/j in context _IF_
> the leading digit is written since it would otherwise be a syntax error
> to follow a constant w/ a variable name w/o an intervening operator--but
> who routinely writes 1i or 1j in place of just i or j (at least w/o
> having been warned)? I think it still worth making the point...
>
> I don't know if TMW has added the clarification to the documentation or
> not; it's not in this release, anyway.

 >> i
i =
      1
 >> 1+i
ans =
      2
 >> clear i
 >> 1+i
ans =
    1.0000 + 1.0000i
 >> i+j
ans =
         0 + 2.0000i
 >> i=1
i =
      1
 >> i+j
ans =
    1.0000 + 1.0000i
 >>

The couple of cases above can be difficult to track down in code.

I personally think it was probably a bad idea to define i and j as
sqrt(-1) as functions given that Matlab silently allows aliasing of
function names w/ variables. It would have been "more better" to have
required the explicit 1i notation for the imaginary constant. Then the
issue wouldn't/couldn't arise.

I will grant for most newbies it'll not likely happen because most don't
deal in complex variables early on and for the really experienced it's
already known. It's that intermediate or the new user who really is in
an area that does use complex variables that it's likely to occasionally
bite and since it's transparent when it does and code like the snippets
above "look" ok, it can be a debugging hole...

--

Subject: fprintf is not reading the \n

From: Nasser M. Abbasi

Date: 30 Jun, 2012 18:48:38

Message: 16 of 29

On 6/30/2012 1:28 PM, dpb wrote:

> I personally think it was probably a bad idea to define i and j as
> sqrt(-1) as functions given that Matlab silently allows aliasing of
> function names w/ variables.

Ofcourse it is bad. before these can be overwritten. That is
why good code should not depend on i and j as being sqrt(-1) but
should be written in defensive way. Either write i=sqrt(-1)
explicitly inside the local function before using i, or better,
just use sqrt(-1) unless it is used in many different
places in the equation.

> It would have been "more better" to have
> required the explicit 1i notation for the imaginary constant. Then the
> issue wouldn't/couldn't arise.
>

That is also bad. I can still write li='micky mouse', and we
are back to square one.

The correct solution is like Mathematica, and Maple where these
constants are READ ONLY.

Maple:
-------------
> restart;
> I*I;
         -1
> I:=3;
Error, illegal use of an object as a name
>
-------------------

Mathematica:
----------------------
In[39]:= I*I
Out[39]= -1

In[40]:= I=3
During evaluation of In[40]:= Set::wrsym: Symbol I is Protected. >>
Out[40]= 3
---------------------

All the solutions given as like putting bandits all over trying
to close one hole here and another there.

The solution should instead go back to the basic problem.

Matlab should have READ only reserved symbols. This way, it
can use i and j or anything else it wants, as long as the
user can't modify them, at least not by mistake.

Just my 1.5 cents (in case someone does not agree :)

--Nasser

Subject: fprintf is not reading the \n

From: dpb

Date: 30 Jun, 2012 19:14:42

Message: 17 of 29

On 6/30/2012 1:48 PM, Nasser M. Abbasi wrote:
> On 6/30/2012 1:28 PM, dpb wrote:
>
>> I personally think it was probably a bad idea to define i and j as
>> sqrt(-1) as functions given that Matlab silently allows aliasing of
>> function names w/ variables.
>
> Of course it is bad. before these can be overwritten. That is
> why good code should not depend on i and j as being sqrt(-1) but
> should be written in defensive way. Either write i=sqrt(-1)
> explicitly inside the local function before using i, or better,
> just use sqrt(-1) unless it is used in many different
> places in the equation.

Or, use my suggestion which is to not use 'i' or 'j' as real (loop)
variables--cleaner, imo.

>> It would have been "more better" to have
>> required the explicit 1i notation for the imaginary constant. Then the
>> issue wouldn't/couldn't arise.
>>
>
> That is also bad. I can still write li='micky mouse', and we
> are back to square one.

No you can't. '1i' is not a legal variable name (must begin w/ a
alphabetic character). Actually try it and see... :)

1i (or any legal numeric string including floating point values) is
unequivocally an imaginary number.

> The correct solution is like Mathematica, and Maple where these
> constants are READ ONLY.
>
> Maple:
> -------------
>> restart;
>> I*I;
> -1
>> I:=3;
> Error, illegal use of an object as a name
>>
> -------------------
>
> Mathematica:
> ----------------------
> In[39]:= I*I
> Out[39]= -1
>
> In[40]:= I=3
> During evaluation of In[40]:= Set::wrsym: Symbol I is Protected. >>
> Out[40]= 3
> ---------------------
>
> All the solutions given as like putting bandits all over trying
> to close one hole here and another there.

Those "bandits" must be more than of the one-armed type... :)

> The solution should instead go back to the basic problem.
>
> Matlab should have READ only reserved symbols. This way, it
> can use i and j or anything else it wants, as long as the
> user can't modify them, at least not by mistake.
>
> Just my 1.5 cents (in case someone does not agree :)

I don't know Maple nor Mathematica well enough to know--if I (or J) are
indeed defined constants (PARAMETER in Fortran) and one can not have a
variable of that name no matter what, I don't really like that as a
solution, either. I would much rather write the numeric constant or
have a more unique name than those. Of course, you gave the uppercase
symbol and if they are both also case-retentive, and i/j are ordinary
variables, that's one way that is at least reasonably workable. I think
it would be a partial solution in Matlab although it wouldn't be
inviolate; the uppercase convention for constants isn't unique nor
particularly new.

I don't argue in general that there would be/is a place for the
equivalent of a Fortran PARAMETER in any programming language; Matlab
not excepted. What are the full difficulties in implementation that
have prevented TMW from doing so I've not even tried to contemplate in
detail.

My point remains that Matlab is what it is and has its own particular
syntax and the possible confounding of i/j is an area new users should
be made aware of. After that, they can deal with it as they see fit.

--

Subject: fprintf is not reading the \n

From: dpb

Date: 30 Jun, 2012 22:17:21

Message: 18 of 29

On 6/30/2012 12:57 PM, Doug Schwarz wrote:
...

> Also, dpb, you have mentioned that you use an old version of MATLAB.
> Newer versions can use 1i or 1j for the imaginary unit (not sure how far
> back that goes). I use i and j all the time as variables and use 1i or
> sqrt(-1) for the imaginary unit. It has never confused anyone.

There's a big difference between 1i and just i, though that I sorta'
muddied over in other reply.

The '1i' is a complex constant (w/ zero real component) and the i in
that expression has nothing directly to do w/ the implicit i defined as
an inline function--instead it is part of the character string
representation of the complex value. Parsing a complex constant has
been inherent in the Matlab interpreter from day one.

The expression '1i' or its ilk is illegal syntax if i were the implicit
definition; it would have to be written as 1*i to be that one.

 >> clear i % go back to native for sure
 >> I=i
I =
         0 + 1.0000i
 >> i=1; % now alias i
 >> 1i
ans =
         0 + 1.0000i
 >> 1I
??? 1I
      |
Error: Missing operator, comma, or semicolon.

 >> 1*i
ans =
      1
 >> clear i
 >> 1*i
ans =
         0 + 1.0000i
 >>

And, I know none of this is news to you although it's easy to overlook
the difference between the two usages of 'i' outlined.

After reflection, I'm not sure it wouldn't have been better off to have
simply forgotten about defining the functions entirely.

--

Subject: fprintf is not reading the \n

From: Doug Schwarz

Date: 1 Jul, 2012 03:26:13

Message: 19 of 29

In article <jsnttk$7cs$1@speranza.aioe.org>, dpb <none@non.net> wrote:

[snip]

> Parsing a complex constant has
> been inherent in the Matlab interpreter from day one.

[By that dpb means an imaginary constant such as '2i' or a complex one
such as '3+2i' with no explicit multiplication.]

It goes back farther than I originally thought, but it doesn't go back
to version 3 -- at least there is no mention of this in the manual from
1989. I couldn't find my version 4 manuals so I don't know if you could
do it there. By version 5 it was definitely a feature.

[snip]

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: fprintf is not reading the \n

From: dpb

Date: 1 Jul, 2012 04:00:02

Message: 20 of 29

On 6/30/2012 10:26 PM, Doug Schwarz wrote:
> In article<jsnttk$7cs$1@speranza.aioe.org>, dpb<none@non.net> wrote:
>
> [snip]
>
>> Parsing a complex constant has
>> been inherent in the Matlab interpreter from day one.
>
> [By that dpb means an imaginary constant such as '2i' or a complex one
> such as '3+2i' with no explicit multiplication.]
>
> It goes back farther than I originally thought, but it doesn't go back
> to version 3 -- at least there is no mention of this in the manual from
> 1989. I couldn't find my version 4 manuals so I don't know if you could
> do it there. By version 5 it was definitely a feature.
>
> [snip]

Oh, but it does...they didn't make it very obvious but if you have the
printed manual in the "Using Matlab" manual in Chap 10, p 10-20 the
table of Data Types under 'double' you can find hidden away the following...

Class Example Description
... ... ...
double [1 2;3 4] Double precision numeric array (this is the
               5+6i most common MATLAB variable type).

I believe (but admit I'm not positive as Ver 3 was the first version I
really recall using enough to become at all familiar with) that the
original implementation used FORTRAN COMPLEX as the default type and
that that carried over. Perhaps the Cleve white paper on history
mentions it; I didn't take the time to go look at the moment but complex
goes with the "wayback" machine... :)

Chapter 9, page 9-5 mentions storage scheme for sparse matrices w/
complex elements although it doesn't mention the technique for creating
them there.

--

Subject: fprintf is not reading the \n

From: dpb

Date: 1 Jul, 2012 13:08:33

Message: 21 of 29

On 6/30/2012 11:00 PM, dpb wrote:
> On 6/30/2012 10:26 PM, Doug Schwarz wrote:
>> In article<jsnttk$7cs$1@speranza.aioe.org>, dpb<none@non.net> wrote:
>>
>> [snip]
>>
>>> Parsing a complex constant has
>>> been inherent in the Matlab interpreter from day one.
>>
>> [By that dpb means an imaginary constant such as '2i' or a complex one
>> such as '3+2i' with no explicit multiplication.]
>>
>> It goes back farther than I originally thought, but it doesn't go back
>> to version 3 -- at least there is no mention of this in the manual from
>> 1989. ...
>>
>> [snip]
>
> Oh, but it does...they didn't make it very obvious but if you have the
> printed manual in the "Using Matlab" manual in Chap 10, p 10-20 the
> table of Data Types under 'double' you can find hidden away the
> following...
>
> Class Example Description
> .... ... ...
> double [1 2;3 4] Double precision numeric array (this is the
> 5+6i most common MATLAB variable type).
...

OK, just ahead of the above on p 10-18 in the table "Special Values" are
i and j. The example at the bottom of the page uses

A=[3 + 2i 7-8i];

and says that is a use of the above. And, on reflection, given the way
the Matlab parser works, it undoubtedly does call the inline version
that is special-cased to recognize the complex constant form of the
imaginary unit rather than there being something more like an i/o
conversion specifier as I was making it out to be--that's just "the
MATLAB way". But the effect is the same in that that's the only way the
literal constant usage can be consistently the complex unit when the
user has aliased them w/ any other value; real or complex. It also
shows up as special-cased in parsing in such usage as 'ii' that if were
to be interpreted as two 'i's could be expected to be -1 but is instead,
of course, a new variable named 'ii'.

All in all, because of the way MATLAB works in the parser, I've now
concluded in complete reversal of my former position ( :) )--they _HAVE_
to be defined as the inline functions and the parser has to recognize
them in context for even the 2i form to work. Consequently, I am now
even more confident that it is syntax from the very beginning...

How's that for riding both sides... :)

--

Subject: fprintf is not reading the \n

From: dpb

Date: 1 Jul, 2012 14:21:59

Message: 22 of 29

On 7/1/2012 8:08 AM, dpb wrote:
...

> How's that for riding both sides... :)

Well, phooey... :(

I just went to put the manuals away and w/ my glasses on and the light
of day I realize the '3' I thought was on the cover is actually a '5'
and I don't seem to have the _real_ V3 doc's at hand, either...

So, I guess we're still at an impasse as to how far back complex
variables were introduced but I surely have always thought MATLAB has
known about them since early on but it's certainly not possible to
accurately recall what was what when any longer.

Sorry for all the mistaken postings...Steven will have fun trying to
sort this all out if he stumbles across it won't he? Hopefully not.

Anyway, whenever it was introduced, the parsing does have to be unique
for the behavior as presently observed...

--

Subject: fprintf is not reading the \n

From: Doug Schwarz

Date: 2 Jul, 2012 00:40:11

Message: 23 of 29

In article <jspme8$pmv$1@speranza.aioe.org>, dpb <none@non.net> wrote:

> On 7/1/2012 8:08 AM, dpb wrote:
> ...
>
> > How's that for riding both sides... :)
>
> Well, phooey... :(
>
> I just went to put the manuals away and w/ my glasses on and the light
> of day I realize the '3' I thought was on the cover is actually a '5'
> and I don't seem to have the _real_ V3 doc's at hand, either...
>
> So, I guess we're still at an impasse as to how far back complex
> variables were introduced but I surely have always thought MATLAB has
> known about them since early on but it's certainly not possible to
> accurately recall what was what when any longer.
>
> Sorry for all the mistaken postings...Steven will have fun trying to
> sort this all out if he stumbles across it won't he? Hopefully not.
>
> Anyway, whenever it was introduced, the parsing does have to be unique
> for the behavior as presently observed...
>
> --



I wasn't talking about complex variables, I was talking about the parser
understanding the construction '2i'. Certainly, MATLAB has had complex
variables from the very beginning. What I saw in the 1989 manual was
the section on how to construct a complex number. In there, the
instruction was quite explicit that you used something of the form

  3 + 2*i

and the possibility of constructing it with

  3 + 2i

was not mentioned, I believe because it was not valid syntax at that
time. In the former construction, clearly 'i' is the result of the 'i'
function that returns sqrt(-1). In the latter construction, the parser
itself knows what 2i means and the 'i' function is not accessed. '2i'
will produce the intended value no matter what value has been assigned
to the variable 'i'.

Therefore (and this is my main point, trivial though it is), the
admonition not to use 'i' as a variable is somewhat obsolete since even
if you still need the imaginary unit you can get it with '1i' and avoid
any ambiguity or error. Rather than advise people not to use 'i' as a
variable, I would prefer to advise them to construct imaginary numbers
with the '1i' form. I am not going to advise anyone to do something
that I violate on a daily basis.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: fprintf is not reading the \n

From: dpb

Date: 2 Jul, 2012 01:04:40

Message: 24 of 29

On 7/1/2012 7:40 PM, Doug Schwarz wrote:
...

> I wasn't talking about complex variables, I was talking about the parser
> understanding the construction '2i'. Certainly, MATLAB has had complex
> variables from the very beginning. What I saw in the 1989 manual was
> the section on how to construct a complex number. In there, the
> instruction was quite explicit that you used something of the form
>
> 3 + 2*i
>
> and the possibility of constructing it with
>
> 3 + 2i
>
> was not mentioned, I believe because it was not valid syntax at that
> time. In the former construction, clearly 'i' is the result of the 'i'
> function that returns sqrt(-1). In the latter construction, the parser
> itself knows what 2i means and the 'i' function is not accessed. '2i'
> will produce the intended value no matter what value has been assigned
> to the variable 'i'.
>
> Therefore (and this is my main point, trivial though it is), the
> admonition not to use 'i' as a variable is somewhat obsolete since even
> if you still need the imaginary unit you can get it with '1i' and avoid
> any ambiguity or error. Rather than advise people not to use 'i' as a
> variable, I would prefer to advise them to construct imaginary numbers
> with the '1i' form. I am not going to advise anyone to do something
> that I violate on a daily basis.

OK; I had made the assumption that the implicit interpretation was also
there from the introduction of complex variables....but, agreed I don't
know that to be so.

I guess I can live w/ the other recommendation as well; I violate it as
well so it's probably overkill to expect others to avoid it.

I think the possible difficult to find use of i in complex expressions
when it has been utilized elsewhere out of habit is a weakness in the
language still, however. But, I'll admit I hadn't really thought thru
about the explicit construction of the value-i form as a foolfproof way
to avoid the issue.

So, I'll try to modify/temper my admonitions... :)

--

Subject: fprintf is not reading the \n

From: Steven_Lord

Date: 2 Jul, 2012 04:12:46

Message: 25 of 29



"dpb" <none@non.net> wrote in message news:jspme8$pmv$1@speranza.aioe.org...
> On 7/1/2012 8:08 AM, dpb wrote:
> ...
>
>> How's that for riding both sides... :)
>
> Well, phooey... :(
>
> I just went to put the manuals away and w/ my glasses on and the light of
> day I realize the '3' I thought was on the cover is actually a '5' and I
> don't seem to have the _real_ V3 doc's at hand, either...
>
> So, I guess we're still at an impasse as to how far back complex variables
> were introduced but I surely have always thought MATLAB has known about
> them since early on but it's certainly not possible to accurately recall
> what was what when any longer.
>
> Sorry for all the mistaken postings...Steven will have fun trying to sort
> this all out if he stumbles across it won't he? Hopefully not.

In the future, as soon as you realize the conversation is going off on a
tangent like this, please could you move the tangent to a separate thread?
That way I don't have to try to detangle the original discussion and the
side thread ;)

I will try to dig through tomorrow; it's too late tonight to give it a
thorough reading.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: fprintf is not reading the \n

From: dpb

Date: 2 Jul, 2012 12:29:49

Message: 26 of 29

On 7/1/2012 11:12 PM, Steven_Lord wrote:
...

> I will try to dig through tomorrow; it's too late tonight to give it a
> thorough reading.

I wouldn't bother, Steven...it's not worth the effort. I wasn't
intending that as a serious comment.

The only question if it isn't too time consuming would be was the '2i'
nomenclature in earliest versions or was that introduced later? I have
always presumed it was there from the git-go as a consequence of parsing
input for complex constants (a la FORTRAN) but as Doug notes, he
couldn't find a specific reference in his earliest documentation at hand.

My screwup really began when wasn't looking at what I thought I was
(altho I should have realized it when it had stuff in there on cells and
structures, I just wasn't thinking about that). It was late here and I
was sitting up in the dark watching lightning to make sure if started
any grass fires we were at least going to be able to get out if
necessary. The reason for being in the dark was so any outside light
would be more obvious quickly...fortunately, no fires; unfortunately, no
rain more than a sprinkle, either.)

--

Subject: fprintf is not reading the \n

From: anyone

Date: 2 Jul, 2012 13:36:47

Message: 27 of 29

On Sat, 30 Jun 2012 13:01:32 -0500, dpb wrote:

> On 6/30/2012 8:15 AM, anyone wrote:
> ...
>
>> "...something like..." does not imply 'the only way', does it?
>
> No, but it doesn't lead down the path of righteousness, either... :)
>
> Why give a newbie that kind of guidance as first choice?

I'd thought that would be obvious, but since it wasn't:

1. Because it is an answer.
2. Because it will work.
3. Because it is general.
4. Because the user wouldn't need to know much about what happens 'under
the hood' to see that it works

And before you ask, the question was -not- 'what is the simplest, most
efficient way to...'

Subject: fprintf is not reading the \n

From: anyone

Date: 2 Jul, 2012 13:39:20

Message: 28 of 29

On Sat, 30 Jun 2012 13:28:06 -0500, dpb wrote:

> On 6/30/2012 1:14 PM, dpb wrote:
[...]
> The couple of cases above can be difficult to track down in code.

If you're a sloppy coder, yes.

Subject: fprintf is not reading the \n

From: Steven_Lord

Date: 2 Jul, 2012 15:00:38

Message: 29 of 29



"dpb" <none@non.net> wrote in message news:jss47t$g8n$1@speranza.aioe.org...
> On 7/1/2012 11:12 PM, Steven_Lord wrote:
> ...
>
>> I will try to dig through tomorrow; it's too late tonight to give it a
>> thorough reading.
>
> I wouldn't bother, Steven...it's not worth the effort. I wasn't intending
> that as a serious comment.
>
> The only question if it isn't too time consuming would be was the '2i'
> nomenclature in earliest versions or was that introduced later?

I'm not sure if it was in Cleve's original version or version 1.0 from
MathWorks, but it's been around for a LOOOONG time.

I can trace it back to at least 1992 and MATLAB 4.0 based on the help text
in i.m using the 2i syntax; there's no such file in MATLAB 3.5, and I'm not
certain if I could find a machine old enough to run that version to check if
it works there. If it isn't already, next year that syntax will be old
enough to purchase alcohol in the United States ;)

> I have always presumed it was there from the git-go as a consequence of
> parsing input for complex constants (a la FORTRAN) but as Doug notes, he
> couldn't find a specific reference in his earliest documentation at hand.

Maybe, but I can't find a reference that old, at least not without doing
some serious archaeology. Or asking Cleve, Loren, or one of the other
developers I know that have been here since at least 1992 and hoping they
remember.

> My screwup really began when wasn't looking at what I thought I was (altho
> I should have realized it when it had stuff in there on cells and
> structures, I just wasn't thinking about that). It was late here and I
> was sitting up in the dark watching lightning to make sure if started any
> grass fires we were at least going to be able to get out if necessary.
> The reason for being in the dark was so any outside light would be more
> obvious quickly...fortunately, no fires; unfortunately, no rain more than
> a sprinkle, either.)

Ah, you're out in the Colorado area? Stay safe, and may you have wet (but
not thunderstormy) weather soon.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for 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