0

I am having difficulty in reading data from a .txt file using Matlab.

I have to create a 200x128 dimension array in Matlab, using the data from the .txt file. This is a repetitive task, and needs automation.

Each row of the .txt file is a complex number of form a+ib, which is of form a[space]b. A sample of my text file :

Link to text file : Click Here

(0)

1.2 2.32222

2.12 3.113

.

.

.

3.2 2.22

(1)

4.4 3.4444

2.33 2.11

2.3 33.3

.

.

.

(2)

.

.

(3)

.

.

(199)

.

.

I have numbers of rows (X), inside the .txt file surrounded by brackets. My final matrix should be of size 200x128. After each (X), there are exactly 128 complex numbers.

2 Answers 2

1

Here is what I would do. First thing, delete the "(0)" types of lines from your text file (could even use a simple shells script for that). This I put into the file called post2.txt.

# First, load the text file into Matlab:
A = load('post2.txt');

# Create the imaginary numbers based on the two columns of data:
vals = A(:,1) + i*A(:,2);

# Then reshape the column of complex numbers into a matrix
mat = reshape(vals, [200,128]);

The mat will be a matrix of 200x128 complex data. Obviously at this point you can put a loop around this to do this multiple times.

Hope that helps.

Sign up to request clarification or add additional context in comments.

3 Comments

I am not so good with shell scripts, I ended up writing a 25 line python code to create post2. If you could share how to do it on shell, that will be helpful in future. Thanks again! Also, your code should have been reshape(vals, [128,200])' .
@Suddev: If you are on a unix based system you can just use an inverted grep such as: grep -v '^(' post.txt > post2.txt This outputs all the lines that don't start with a bracket.
i was on windows, i tried what you said on unix based machine. It works - thanks again!
1

You can read the data in using the following function:

function data = readData(aFilename, m,n)

% if no parameters were passed, use these as defaults:
if ~exist('aFilename', 'var')
    m = 128;
    n = 200;
    aFilename = 'post.txt';
end

% init some stuff:
data= nan(n, m);
formatStr = [repmat('%f', 1, 2*m)];

% Read in the Data:
fid = fopen(aFilename);
for ind = 1:n
    lineID = fgetl(fid);
    dataLine = fscanf(fid, formatStr);
    dataLineComplex = dataLine(1:2:end) + dataLine(2:2:end)*1i;
    data(ind, :) = dataLineComplex;
end
fclose(fid);

(edit) This function can be improved by including the (1) parts in the format string and throwing them out:

function data = readData(aFilename, m,n)

% if no parameters were passed, use these as defaults:
if ~exist('aFilename', 'var')
    m = 128;
    n = 200;
    aFilename = 'post.txt';
end

% init format stuff:
formatStr = ['(%*d)\n' repmat('%f%f\n', 1, m)];

% Read in the Data:
fid = fopen(aFilename);

data = fscanf(fid, formatStr);
data = data(1:2:end) + data(2:2:end)*1i;
data = reshape(data, n,m);

fclose(fid);

6 Comments

I am going to combine your answer & @brechmos answer to solve this. I hope I can do it soon.
@Steve, why use "nan(n, m)". Might be better to do "zeros(n, m)". That is more conventional, but maybe I am missing something. (Depends on assumptions I suppose). I suspect looping like that is going to be less efficient than using the internal load function.
@Suddev: If you are on a unix based system you can just use an inverted grep such as: grep -v '^(' post.txt > post2.txt This outputs all the lines that don't start with a bracket.
@brechmos Regarding the load command it is not clear which is faster though you very well could be correct. MATLAB's load is built for flexibility while the loop is slow for large values. I wanted to use the format string formatStr = ['(%*d)' repmat('%f', 1, 2*m)]; and then load the entire file at once----probably quite fast, but it hung after the first row record so I threw it in a loop. Since the files are small there is only a bit of penalty, anyway. To get that particular format to work this seemed like a quick enough solution.
@brechmos As for the nan values, I often like to do that so that if the code has an indexing error those nan values provide a wicked, unpleasant indicator for future processing. Also, I fixed the format string by adding \n and added a loop-less example to the answer. For you <3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.