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:
reading complex marix from file

Subject: reading complex marix from file

From: G0Y

Date: 13 Nov, 2008 21:36:38

Message: 1 of 10

Hi,

How can I load a file, which has complex elements?

I do the following, but I do not know how to define the "format" part of the fscanf.

>> fin = fopen('vector.txt', 'r');
>> A = fscanf(fin,'%f+f%i' ,[8,3]);

This '%f+%fi' does not work.

Subject: reading complex marix from file

From: Walter Roberson

Date: 13 Nov, 2008 22:20:16

Message: 2 of 10

G0Y wrote:

> How can I load a file, which has complex elements?

> I do the following, but I do not know how to define the "format" part of the fscanf.
>
>>> fin = fopen('vector.txt', 'r');
>>> A = fscanf(fin,'%f+f%i' ,[8,3]);

> This '%f+%fi' does not work.

Please recheck your code. The code you quoted has a format of
'%f+f%i' (from your code) instead of
'%f+%fi' (from your commentary)

Notice the change in position of the second '%'. That would cause it to look for a
literal '+f' instead of for a literal '+' followed by a number followed by a
literal 'i'.

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: reading complex marix from file

From: G0Y

Date: 14 Nov, 2008 07:04:53

Message: 3 of 10

Actually the contets of my vector.txt file (one cloumns from the 1000):

 0.998697+0i
 0+0i
 0.0510287+0i
 0+0i
 0+0i
 0+0i
 0+0i
 0+0i

I would like to read in with fscanf or other way if it is possible.

>> fin = fopen('vector.txt', 'r');

>>> A = fscanf(fin,'I do not know this part...' ,[4,2]);


Message was edited by: G0Y

Subject: reading complex marix from file

From: Andres

Date: 14 Nov, 2008 15:33:02

Message: 4 of 10

G0Y <gbencedi@gmail.com> wrote in message <5212991.1226646349925.JavaMail.jakarta@nitrogen.mathforum.org>...
> Actually the contets of my vector.txt file (one cloumns from the 1000):
>
> 0.998697+0i
> 0+0i
> 0.0510287+0i
> 0+0i
> 0+0i
> 0+0i
> 0+0i
> 0+0i
>
> I would like to read in with fscanf or other way if it is possible.
>
> >> fin = fopen('vector.txt', 'r');
>
> >>> A = fscanf(fin,'I do not know this part...' ,[4,2]);
>
>
> Message was edited by: G0Y

Hi,
did you consider Walter's reply?

c = sscanf('1+2i 3-4.5i -6.7+8i','%f %fi')

c =

    1.0000
    2.0000
    3.0000
   -4.5000
   -6.7000
    8.0000

That's just what you want, isn't it?
Hth
Andres

Subject: reading complex marix from file

From: G0Y

Date: 14 Nov, 2008 18:42:46

Message: 5 of 10

Not really.

Let us clarify: I have a .txt file containing complex numbers in one column. I want to read it into a matrix. Anyway as I see there is no chance to fulfill this method :((

>> %here I open the .txt file to read
>> fin = fopen('vec.txt', 'r');
>> %then I would like to store these COMPLEX numbers in a >> %matrix
>> A=fscanf(fin, 'format' , [size]);

Subject: reading complex marix from file

From: dpb

Date: 14 Nov, 2008 18:55:43

Message: 6 of 10

G0Y wrote:
> Not really.
>
> Let us clarify: I have a .txt file containing complex numbers in one
column. I want to read it into a matrix. Anyway as I see there is no
chance to fulfill this method :((

 >> c = complex(sscanf('1+2i 3-4.5i -6.7+8i','%f %fi',[2,3])')
c =
     1.0000 2.0000
     3.0000 -4.5000
    -6.7000 8.0000

PS. Please answer posts after previous comments -- makes having a
conversation _much_ easier... :)

pps. Leaving some context (enough to follow) would also be good.

--

Subject: reading complex marix from file

From: Walter Roberson

Date: 14 Nov, 2008 19:10:35

Message: 7 of 10

G0Y wrote:
> Not really.
>
> Let us clarify: I have a .txt file containing complex numbers in one column. I want to read
> it into a matrix. Anyway as I see there is no chance to fulfill this method :((

You are correct, Matlab does not offer any method to read complex numbers directly
in to a matrix. If that is your requirement, then Sorry, you will have to find a different
programming language.

It is, however, fairly easy to program around the lack if you are willing to use
a temporary variable. Building about Andres' example:

c = sscanf('1+2i 3-4.5i -6.7+8i','%f %fi'); %get some data in

t = reshape(c,2,[]); %row 1 is real parts, row 2 is complex parts
TheMatrix = reshape(complex(t(1,:),t(2,:)), MatrixWidth, MatrixHeight); %make complex and reshape

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: reading complex marix from file

From: Walter Roberson

Date: 14 Nov, 2008 19:30:21

Message: 8 of 10

dpb wrote:
> G0Y wrote:
>> Not really.
>>
>> Let us clarify: I have a .txt file containing complex numbers in one
> column. I want to read it into a matrix. Anyway as I see there is no
> chance to fulfill this method :((

> >> c = complex(sscanf('1+2i 3-4.5i -6.7+8i','%f %fi',[2,3])')
> c =
> 1.0000 2.0000
> 3.0000 -4.5000
> -6.7000 8.0000

I believe that G0Y would instead be hoping for the answer that would be given by

>> t = sscanf('1+2i 3-4.5i -6.7+8i', '%f %fi',[2 3]).';
>> c = complex(t(:,1),t(:,2))

c =

                          1 + 2i
                          3 - 4.5i
                       -6.7 + 8i

Subject: reading complex marix from file

From: dpb

Date: 14 Nov, 2008 19:41:27

Message: 9 of 10

Walter Roberson wrote:
> dpb wrote:
>> G0Y wrote:
>>> Not really.
>>>
>>> Let us clarify: I have a .txt file containing complex numbers in one
>> column. I want to read it into a matrix. Anyway as I see there is no
>> chance to fulfill this method :((
>
>> >> c = complex(sscanf('1+2i 3-4.5i -6.7+8i','%f %fi',[2,3])')
>> c =
>> 1.0000 2.0000
>> 3.0000 -4.5000
>> -6.7000 8.0000
>
> I believe that G0Y would instead be hoping for the answer that would be
> given by
>
>>> t = sscanf('1+2i 3-4.5i -6.7+8i', '%f %fi',[2 3]).';
>>> c = complex(t(:,1),t(:,2))
...

Yeah, should've read complex() helpfile for refresher--I was thinking it
would take the two alternating components of a automagically -- seems a
little brain-dead in its operation but guess it does follow Fortran
CMPLX() intrinsic on reflection from which (I presume) it was derived...

--

Subject: reading complex marix from file

From: Anver HIsham

Date: 10 Jan, 2012 16:02:08

Message: 10 of 10

[st,A]=system('cat vector.txt');
A=str2num(A);


G0Y <gbencedi@gmail.com> wrote in message <7185560.1226612229256.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hi,
>
> How can I load a file, which has complex elements?
>
> I do the following, but I do not know how to define the "format" part of the fscanf.
>
> >> fin = fopen('vector.txt', 'r');
> >> A = fscanf(fin,'%f+f%i' ,[8,3]);
>
> This '%f+%fi' does not work.

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