I am trying to take a data that's in a matrix in matlab and
output it to a file. In each cell there is a complex
number. Is there a way to write this complex number to a
file? and maintain the precision, or change the precision?
It is definitely possible to write complex numbers to a text file, but the
exact syntax depends on how you want to format the numbers.
1. Do you want to show the numbers as strings using "i" or "j"
to represent the imaginary unit? For example:
2.512 + 3.718i OR
2.512 + 3.718j
2. How do you want the numbers delimited? In other words, do
you want to separate each element with a space, a comma, a
tab, or some other character?
Thanks.
Rick
"Anthony " <anthony.campos@baesystems.com> wrote in message
news:f9fq2l$2vi$1@fred.mathworks.com...
>I am trying to take a data that's in a matrix in matlab and
> output it to a file. In each cell there is a complex
> number. Is there a way to write this complex number to a
> file? and maintain the precision, or change the precision?
It would be nice to have one complex number per row, in the
following format:
a + bi
"Rick Rosson" <rrosson@mathworks.com> wrote in message
<f9fsgh$a17$1@fred.mathworks.com>...
> Hi Anthony,
>
> It is definitely possible to write complex numbers to a
text file, but the
> exact syntax depends on how you want to format the
numbers.
>
> 1. Do you want to show the numbers as strings using "i"
or "j"
> to represent the imaginary unit? For example:
>
> 2.512 + 3.718i OR
> 2.512 + 3.718j
>
> 2. How do you want the numbers delimited? In other
words, do
> you want to separate each element with a space, a
comma, a
> tab, or some other character?
>
> Thanks.
>
> Rick
>
>
> "Anthony " <anthony.campos@baesystems.com> wrote in
message
> news:f9fq2l$2vi$1@fred.mathworks.com...
> >I am trying to take a data that's in a matrix in matlab
and
> > output it to a file. In each cell there is a complex
> > number. Is there a way to write this complex number to a
> > file? and maintain the precision, or change the
precision?
>
>
for col = 1:N
fprintf(fid,'%f + %f%c
',real(X(row,col)),imag(X(row,col)),imchar);
end
fprintf(fid,'\n');
end
fclose(fid);
where 'X' is an array of complex-valued numbers.
Rick
"Anthony " <anthony.campos@baesystems.com> wrote in message
news:f9fq2l$2vi$1@fred.mathworks.com...
>I am trying to take a data that's in a matrix in matlab and
> output it to a file. In each cell there is a complex
> number. Is there a way to write this complex number to a
> file? and maintain the precision, or change the precision?
for k = 1:L
fprintf(fid,'%f + %f%c ',real(X(k)),imag(X(k)),imchar);
fprintf(fid,'\n');
end
fclose(fid);
I hope that helps.
Thanks.
Rick
"Anthony " <anthony.campos@baesystems.com> wrote in message
news:f9fstg$ft6$1@fred.mathworks.com...
> It would be nice to have one complex number per row, in the
> following format:
> a + bi
>
> "Rick Rosson" <rrosson@mathworks.com> wrote in message
> <f9fsgh$a17$1@fred.mathworks.com>...
>> Hi Anthony,
>>
>> It is definitely possible to write complex numbers to a
> text file, but the
>> exact syntax depends on how you want to format the
> numbers.
>>
>> 1. Do you want to show the numbers as strings using "i"
> or "j"
>> to represent the imaginary unit? For example:
>>
>> 2.512 + 3.718i OR
>> 2.512 + 3.718j
>>
>> 2. How do you want the numbers delimited? In other
> words, do
>> you want to separate each element with a space, a
> comma, a
>> tab, or some other character?
>>
>> Thanks.
>>
>> Rick
>>
>>
>> "Anthony " <anthony.campos@baesystems.com> wrote in
> message
>> news:f9fq2l$2vi$1@fred.mathworks.com...
>> >I am trying to take a data that's in a matrix in matlab
> and
>> > output it to a file. In each cell there is a complex
>> > number. Is there a way to write this complex number to a
>> > file? and maintain the precision, or change the
> precision?
>>
>>
>
That worked great. I had to change the 'w' to 'wt' because
it wasn't showing right in notepad. Thanks for your help
"Rick Rosson" <rrosson@mathworks.com> wrote in message
<f9ftl6$qgi$1@fred.mathworks.com>...
> Hi Anthony,
>
> I send the previous solution before I saw your latest
response. To create a
> file with one number on each line, try the following:
>
>
> imchar = 'i';
>
> L = length(X(:));
>
> filename = 'myData.txt';
> fid = fopen(filename,'w');
>
> for k = 1:L
> fprintf(fid,'%f + %f%c ',real(X(k)),imag(X
(k)),imchar);
> fprintf(fid,'\n');
>
> end
>
> fclose(fid);
>
>
> I hope that helps.
>
> Thanks.
>
> Rick
>
>
>
> "Anthony " <anthony.campos@baesystems.com> wrote in
message
> news:f9fstg$ft6$1@fred.mathworks.com...
> > It would be nice to have one complex number per row, in
the
> > following format:
> > a + bi
> >
> > "Rick Rosson" <rrosson@mathworks.com> wrote in message
> > <f9fsgh$a17$1@fred.mathworks.com>...
> >> Hi Anthony,
> >>
> >> It is definitely possible to write complex numbers to a
> > text file, but the
> >> exact syntax depends on how you want to format the
> > numbers.
> >>
> >> 1. Do you want to show the numbers as strings
using "i"
> > or "j"
> >> to represent the imaginary unit? For example:
> >>
> >> 2.512 + 3.718i OR
> >> 2.512 + 3.718j
> >>
> >> 2. How do you want the numbers delimited? In other
> > words, do
> >> you want to separate each element with a space, a
> > comma, a
> >> tab, or some other character?
> >>
> >> Thanks.
> >>
> >> Rick
> >>
> >>
> >> "Anthony " <anthony.campos@baesystems.com> wrote in
> > message
> >> news:f9fq2l$2vi$1@fred.mathworks.com...
> >> >I am trying to take a data that's in a matrix in
matlab
> > and
> >> > output it to a file. In each cell there is a complex
> >> > number. Is there a way to write this complex number
to a
> >> > file? and maintain the precision, or change the
> > precision?
> >>
> >>
> >
>
>
There is some additional formatting you can do with the FPRINTF function.
Take a look at the help for how to adjust the spacing and precision of the
numerical outputs. Type
doc fprintf
at the command prompt, or search for "fprintf" in the Help Browser.
Rick
"Anthony " <anthony.campos@baesystems.com> wrote in message
news:f9fug7$9lq$1@fred.mathworks.com...
> That worked great. I had to change the 'w' to 'wt' because
> it wasn't showing right in notepad. Thanks for your help
>
> "Rick Rosson" <rrosson@mathworks.com> wrote in message
> <f9ftl6$qgi$1@fred.mathworks.com>...
>> Hi Anthony,
>>
>> I send the previous solution before I saw your latest
> response. To create a
>> file with one number on each line, try the following:
>>
>>
>> imchar = 'i';
>>
>> L = length(X(:));
>>
>> filename = 'myData.txt';
>> fid = fopen(filename,'w');
>>
>> for k = 1:L
>> fprintf(fid,'%f + %f%c ',real(X(k)),imag(X
> (k)),imchar);
>> fprintf(fid,'\n');
>>
>> end
>>
>> fclose(fid);
>>
>>
>> I hope that helps.
>>
>> Thanks.
>>
>> Rick
>>
>>
>>
>> "Anthony " <anthony.campos@baesystems.com> wrote in
> message
>> news:f9fstg$ft6$1@fred.mathworks.com...
>> > It would be nice to have one complex number per row, in
> the
>> > following format:
>> > a + bi
>> >
>> > "Rick Rosson" <rrosson@mathworks.com> wrote in message
>> > <f9fsgh$a17$1@fred.mathworks.com>...
>> >> Hi Anthony,
>> >>
>> >> It is definitely possible to write complex numbers to a
>> > text file, but the
>> >> exact syntax depends on how you want to format the
>> > numbers.
>> >>
>> >> 1. Do you want to show the numbers as strings
> using "i"
>> > or "j"
>> >> to represent the imaginary unit? For example:
>> >>
>> >> 2.512 + 3.718i OR
>> >> 2.512 + 3.718j
>> >>
>> >> 2. How do you want the numbers delimited? In other
>> > words, do
>> >> you want to separate each element with a space, a
>> > comma, a
>> >> tab, or some other character?
>> >>
>> >> Thanks.
>> >>
>> >> Rick
>> >>
>> >>
>> >> "Anthony " <anthony.campos@baesystems.com> wrote in
>> > message
>> >> news:f9fq2l$2vi$1@fred.mathworks.com...
>> >> >I am trying to take a data that's in a matrix in
> matlab
>> > and
>> >> > output it to a file. In each cell there is a complex
>> >> > number. Is there a way to write this complex number
> to a
>> >> > file? and maintain the precision, or change the
>> > precision?
>> >>
>> >>
>> >
>>
>>
>
% the data
fnam='foo.txt'; % <- your file name
rr=pi*(1:10);
ii=1:numel(rr);
c=complex(rr.',ii.');
% the engine
dlmwrite(fnam,c,...
'delimiter','\t',...
'precision','%.4f %.1f i');
% the result
c
type(fnam)
"Rick Rosson" <rrosson@mathworks.com> wrote in message
<f9futi$fmb$1@fred.mathworks.com>...
>
> Glad to help.
>
> There is some additional formatting you can do with the
FPRINTF function.
> Take a look at the help for how to adjust the spacing
and precision of the
> numerical outputs. Type
>
> doc fprintf
>
> at the command prompt, or search for "fprintf" in the
Help Browser.
>
> Rick
>
>
>
> "Anthony " <anthony.campos@baesystems.com> wrote in
message
> news:f9fug7$9lq$1@fred.mathworks.com...
> > That worked great. I had to change the 'w' to 'wt'
because
> > it wasn't showing right in notepad. Thanks for your
help
> >
> > "Rick Rosson" <rrosson@mathworks.com> wrote in message
> > <f9ftl6$qgi$1@fred.mathworks.com>...
> >> Hi Anthony,
> >>
> >> I send the previous solution before I saw your latest
> > response. To create a
> >> file with one number on each line, try the following:
> >>
> >>
> >> imchar = 'i';
> >>
> >> L = length(X(:));
> >>
> >> filename = 'myData.txt';
> >> fid = fopen(filename,'w');
> >>
> >> for k = 1:L
> >> fprintf(fid,'%f + %f%c ',real(X(k)),imag(X
> > (k)),imchar);
> >> fprintf(fid,'\n');
> >>
> >> end
> >>
> >> fclose(fid);
> >>
> >>
> >> I hope that helps.
> >>
> >> Thanks.
> >>
> >> Rick
> >>
> >>
> >>
> >> "Anthony " <anthony.campos@baesystems.com> wrote in
> > message
> >> news:f9fstg$ft6$1@fred.mathworks.com...
> >> > It would be nice to have one complex number per
row, in
> > the
> >> > following format:
> >> > a + bi
> >> >
> >> > "Rick Rosson" <rrosson@mathworks.com> wrote in
message
> >> > <f9fsgh$a17$1@fred.mathworks.com>...
> >> >> Hi Anthony,
> >> >>
> >> >> It is definitely possible to write complex numbers
to a
> >> > text file, but the
> >> >> exact syntax depends on how you want to format the
> >> > numbers.
> >> >>
> >> >> 1. Do you want to show the numbers as strings
> > using "i"
> >> > or "j"
> >> >> to represent the imaginary unit? For example:
> >> >>
> >> >> 2.512 + 3.718i OR
> >> >> 2.512 + 3.718j
> >> >>
> >> >> 2. How do you want the numbers delimited? In
other
> >> > words, do
> >> >> you want to separate each element with a
space, a
> >> > comma, a
> >> >> tab, or some other character?
> >> >>
> >> >> Thanks.
> >> >>
> >> >> Rick
> >> >>
> >> >>
> >> >> "Anthony " <anthony.campos@baesystems.com> wrote in
> >> > message
> >> >> news:f9fq2l$2vi$1@fred.mathworks.com...
> >> >> >I am trying to take a data that's in a matrix in
> > matlab
> >> > and
> >> >> > output it to a file. In each cell there is a
complex
> >> >> > number. Is there a way to write this complex
number
> > to a
> >> >> > file? and maintain the precision, or change the
> >> > precision?
> >> >>
> >> >>
> >> >
> >>
> >>
> >
>
>
I have been loking at you solution for writing complex
data in a file using fprintf. That will work well, if your
imaginary number is always positive.
I am in the same situation, but my imaginary number might
change a sign, then using the solution posted on web, e.g.,
gives me a+-jb, what I will be looking for a-jb
Could you please help, to write complex structure in a
file for negative imaginary number.
Thanks in advance.
Jamie
"Rick Rosson" <rrosson@mathworks.com> wrote in message
<f9futi$fmb$1@fred.mathworks.com>...
>
> Glad to help.
>
> There is some additional formatting you can do with the
FPRINTF function.
> Take a look at the help for how to adjust the spacing
and precision of the
> numerical outputs. Type
>
> doc fprintf
>
> at the command prompt, or search for "fprintf" in the
Help Browser.
>
> Rick
>
>
>
> "Anthony " <anthony.campos@baesystems.com> wrote in
message
> news:f9fug7$9lq$1@fred.mathworks.com...
> > That worked great. I had to change the 'w' to 'wt'
because
> > it wasn't showing right in notepad. Thanks for your
help
> >
> > "Rick Rosson" <rrosson@mathworks.com> wrote in message
> > <f9ftl6$qgi$1@fred.mathworks.com>...
> >> Hi Anthony,
> >>
> >> I send the previous solution before I saw your latest
> > response. To create a
> >> file with one number on each line, try the following:
> >>
> >>
> >> imchar = 'i';
> >>
> >> L = length(X(:));
> >>
> >> filename = 'myData.txt';
> >> fid = fopen(filename,'w');
> >>
> >> for k = 1:L
> >> fprintf(fid,'%f + %f%c ',real(X(k)),imag(X
> > (k)),imchar);
> >> fprintf(fid,'\n');
> >>
> >> end
> >>
> >> fclose(fid);
> >>
> >>
> >> I hope that helps.
> >>
> >> Thanks.
> >>
> >> Rick
> >>
> >>
> >>
> >> "Anthony " <anthony.campos@baesystems.com> wrote in
> > message
> >> news:f9fstg$ft6$1@fred.mathworks.com...
> >> > It would be nice to have one complex number per
row, in
> > the
> >> > following format:
> >> > a + bi
> >> >
> >> > "Rick Rosson" <rrosson@mathworks.com> wrote in
message
> >> > <f9fsgh$a17$1@fred.mathworks.com>...
> >> >> Hi Anthony,
> >> >>
> >> >> It is definitely possible to write complex numbers
to a
> >> > text file, but the
> >> >> exact syntax depends on how you want to format the
> >> > numbers.
> >> >>
> >> >> 1. Do you want to show the numbers as strings
> > using "i"
> >> > or "j"
> >> >> to represent the imaginary unit? For example:
> >> >>
> >> >> 2.512 + 3.718i OR
> >> >> 2.512 + 3.718j
> >> >>
> >> >> 2. How do you want the numbers delimited? In
other
> >> > words, do
> >> >> you want to separate each element with a
space, a
> >> > comma, a
> >> >> tab, or some other character?
> >> >>
> >> >> Thanks.
> >> >>
> >> >> Rick
> >> >>
> >> >>
> >> >> "Anthony " <anthony.campos@baesystems.com> wrote in
> >> > message
> >> >> news:f9fq2l$2vi$1@fred.mathworks.com...
> >> >> >I am trying to take a data that's in a matrix in
> > matlab
> >> > and
> >> >> > output it to a file. In each cell there is a
complex
> >> >> > number. Is there a way to write this complex
number
> > to a
> >> >> > file? and maintain the precision, or change the
> >> > precision?
> >> >>
> >> >>
> >> >
> >>
> >>
> >
>
>
In article <fihp3k$sau$1@fred.mathworks.com>,
Jamie James <jamie.james.nospam@mathworks.com> wrote:
Please do not post your reply at the top of what you are
commenting on: it makes it more difficult to hold a conversation.
>> > "Rick Rosson" <rrosson@mathworks.com> wrote in message
>> > <f9ftl6$qgi$1@fred.mathworks.com>...
>> >> for k = 1:L
>> >> fprintf(fid,'%f + %f%c ',real(X(k)),imag(X
>> > (k)),imchar);
>> >> fprintf(fid,'\n');
>> >>
>> >> end
>I am in the same situation, but my imaginary number might
>change a sign, then using the solution posted on web, e.g.,
>gives me a+-jb, what I will be looking for a-jb
signs = '-++';
for k = 1:L
fprintf(fid, '%f %c %f%c ', real(X(k)), signs(2+sign(imag(X(k)))), ...
imag(X(k)), imchar)
end
fprintf(fid, '\n');
sign(x) is -1, 0, or 1; add 2 to that to take it to the range
1 2 3, which you then index into the signs() table of characters
to print out to indicate the sign.
--
"All is vanity." -- Ecclesiastes
> Hello Rick,
>
> I have been loking at you solution for writing complex
> data in a file using fprintf. That will work well, if your
> imaginary number is always positive.
>
> I am in the same situation, but my imaginary number might
> change a sign, then using the solution posted on web, e.g.,
> gives me a+-jb, what I will be looking for a-jb
>
> Could you please help, to write complex structure in a
> file for negative imaginary number.
fprintf('%f%+fi\n', real(x), imag(x));
The trick is the %+f flag, which says print the sign of the variable,
even if it's plus.
Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyhcj78osx.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "Jamie James" <jamie.james.nospam@mathworks.com> writes:
>
> > Hello Rick,
> >
> > I have been loking at you solution for writing complex
> > data in a file using fprintf. That will work well, if
your
> > imaginary number is always positive.
> >
> > I am in the same situation, but my imaginary number
might
> > change a sign, then using the solution posted on web,
e.g.,
> > gives me a+-jb, what I will be looking for a-jb
> >
> > Could you please help, to write complex structure in a
> > file for negative imaginary number.
>
> fprintf('%f%+fi\n', real(x), imag(x));
>
> The trick is the %+f flag, which says print the sign of
the variable,
> even if it's plus.
>
> -Peter
***********************************************************
Peter,
That works, great. Thanks for the help.
JamieJames
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fihppn$ipt$1@canopus.cc.umanitoba.ca>...
> In article <fihp3k$sau$1@fred.mathworks.com>,
> Jamie James <jamie.james.nospam@mathworks.com> wrote:
>
> Please do not post your reply at the top of what you are
> commenting on: it makes it more difficult to hold a
conversation.
>
> >> > "Rick Rosson" <rrosson@mathworks.com> wrote in
message
> >> > <f9ftl6$qgi$1@fred.mathworks.com>...
>
> >> >> for k = 1:L
> >> >> fprintf(fid,'%f + %f%c ',real(X(k)),imag(X
> >> > (k)),imchar);
> >> >> fprintf(fid,'\n');
> >> >>
> >> >> end
>
> >I am in the same situation, but my imaginary number
might
> >change a sign, then using the solution posted on web,
e.g.,
> >gives me a+-jb, what I will be looking for a-jb
>
> signs = '-++';
>
> for k = 1:L
> fprintf(fid, '%f %c %f%c ', real(X(k)), signs(2+sign
(imag(X(k)))), ...
> imag(X(k)), imchar)
> end
> fprintf(fid, '\n');
>
> sign(x) is -1, 0, or 1; add 2 to that to take it to the
range
> 1 2 3, which you then index into the signs() table of
characters
> to print out to indicate the sign.
> --
> "All is vanity." --
Ecclesiastes
***********************************************************
Hello Walter,
Have a comment about your soultion, it will not work, e.g.,
in a case, if you have a complex number with the negative
sign,e.g., a-jb. Because signs(2+sign(imag(X(k)))) will
return a value of 1 (if the imaginary part is neagtive),so
from the the signs'-++', the '-' will be selected and the
final result will be in the form a- -jb.
On Nov 29, 3:04 pm, "us " <u...@neurol.unizh.ch> wrote:
> "Jamie James"
> <SNIP complex complexity...
>
> a hint:
>
> the utility array2str, which is available on the FEX, most
> likely does exactly what you want
>
> http://www.mathworks.com/matlabcentral/fileexchange/loadFile
> .do?objectId=17484&objectType=FILE
>
> it comes with many options and may serve as a nice front
> end engine for appps that require a certain ascii input...
>
> just a thought
> us
"Anthony " <anthony.campos@baesystems.com> wrote in message
<f9fq2l$2vi$1@fred.mathworks.com>...
> I am trying to take a data that's in a matrix in matlab and
> output it to a file. In each cell there is a complex
> number. Is there a way to write this complex number to a
> file? and maintain the precision, or change the precision?
Hi Anthony
You can use function num2str for this purpose. For example :
x = 1 + 2 * i
fprintf ( 1, 'X = %s\n', num2str ( x ) );
This works for both +ve as well as -ve imaginary parts and
you won't have to do other tricks as mentioned in the other
solutions.
Hope that helps.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.