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:
read floating point from txt file and store in matrix

Subject: read floating point from txt file and store in matrix

From: amira ali

Date: 23 May, 2011 09:39:04

Message: 1 of 3

i use the following codes to read the txt file
fid = fopen('x.txt','r');
B=textscan(fid,'%f ', 'delimiter',',');
C=cell2mat(B);
fclose(fid);

this code give me this result

255
488
61.113
96.789
71.157
0
0
0
0
0
0
0
59.495
98.087
71.069
0
0
0
0


but i need when i arrive to end of line start to to save values in new row not to complete in the same coulmn as above
the shape of text which i need command to store it in matrix as follow

255 (first row)
488 (sec row)
61.1132,96.789,71.1566,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 //end of line(third row)
59.4945,98.0872,71.0687,0,0,0,0,0,0,0 (for row)

Subject: read floating point from txt file and store in matrix

From: Sadik

Date: 25 May, 2011 17:02:04

Message: 2 of 3

Hi Amira,

You could give fgetl a try. This will give you each line separately. You can the store the lines in a cell array using a for loop or a while loop as in while ~feof(fid).

Best.

Subject: read floating point from txt file and store in matrix

From: dpb

Date: 25 May, 2011 17:18:54

Message: 3 of 3

On 5/23/2011 4:39 AM, amira ali wrote:
> i use the following codes to read the txt file
> fid = fopen('x.txt','r');
   B=textscan(fid,'%f ', 'delimiter',',');
> C=cell2mat(B);
   fclose(fid);
>
> this code give me this result

...[a column vector elided for brevity]...

...

> but i need when i arrive to end of line start to to save values in new
> row not to complete in the same coulmn as above
> the shape of text which i need command to store it in matrix as follow
...

I'd just use reshape() as needed on subsets of the vector instead of
trying to do it during input.

--

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