Got Questions? Get Answers.
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:
Data Manipulation (Re-arranging) and Writing to text file

Subject: Data Manipulation (Re-arranging) and Writing to text file

From: Stan

Date: 18 Jun, 2012 03:17:15

Message: 1 of 1

Hello, here’s the details of the problem that I am writing about, and my attempts at a solution:

a. I need to extract elements from rows 1-6 from the first column of a file (input.txt, I’ve loaded the file successfully). This file has numerous columns with 200 rows each. I am interested in only the first column. Then I need to print these extracted elements to row 1 of a text file, separated (delimited) by a tab.

b. I need to then repeat this in succession 10 times.i.e. extract 7-12 and print them on row 2 of the text file (tab delimited), then extract 13-18 and print them on row 3 of the text file (tab delimited).

c. Now, the 11th time (i.e. row 11), I need only 2 elements (rows 61 and 62) to be extracted and printed on row 11. The 4 other spaces (where elements 63, 64, 65, 66 would go) should be left empty (not zeros, but empty).

MY ATTEMPTS:
a. Here’s what I have come up with (neither is printing more than 1 row, but rather just printing the last row that is extracted). I like the first method (taking the transpose, it seems to be working), but I can’t get it to print as I go along through the loop.

First Approach (extract vectors using a loop, use dlmwrite to write them to text file):
eee = importdata('input.txt');
 
for i = 1:6:19
        aa = eee.data(i:i+5,1);
        dlmwrite('test3.txt',aa','\t')
end

Second Approach (extract elements indiviually and write them to the text file using fprintf):
eee = importdata('input.txt');
 
sav_fid = fopen('test4.txt','w');
 
for i = 1:6
        aa(i) = eee.data(i,1);
        fprintf(sav_fid,'%f',aa(i));
end

b. I am thinking I would need to do this:
 for j = 1:6:66

 

 

 end

I would put this around the loop in a. Is this correct or is there is a better way to accomplish this step?

c. If this was a 1-D array, I would do it easily using:

rem = [r1 r2 r3]
A(rem) = []

I’m just not sure if this would work if step b. is not working. Is there a better way to do this?

Regards:
Stan T.

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