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:
text file data into matrix format

Subject: text file data into matrix format

From: ali

Date: 28 Nov, 2012 08:37:21

Message: 1 of 11

hi

i am a beginner in Matlab. i am just stuck in part of my project. i have a text file which has the following format data.


x= 71, y= 7, z= 228

x= 70, y= 5, z= 228

x= 71, y= 5, z= 227

x= 71, y= 6, z= 230

x= 71, y= 6, z= 228

x= 71, y= 7, z= 228


i want make a graph for each axis. for that i have to make a column matrix for x, y and z.

i don't from where to start.

plz any kind of code help would be a great for me.

thank you

rony

Subject: text file data into matrix format

From: Nasser M. Abbasi

Date: 28 Nov, 2012 08:57:04

Message: 2 of 11

On 11/28/2012 2:37 AM, ali wrote:
> hi
>
> i am a beginner in Matlab. i am just stuck in part of my project. i have a
>text file which has the following format data.
>
>
> x= 71, y= 7, z= 228
>
> x= 70, y= 5, z= 228
>
> x= 71, y= 5, z= 227
>
> x= 71, y= 6, z= 230
>
> x= 71, y= 6, z= 228
>
> x= 71, y= 7, z= 228
>
>
> i want make a graph for each axis. for that i have to make a column matrix for x, y and z.

There is nothing really called 'column matrix`. It is called just a matrix.

>
> i don't from where to start.
>
> plz any kind of code help would be a great for me.

If you can change the program or function that was used to generate
the output above so that it writes the output in simpler way,
like this

71 7 228
70 5 228

etc...

it will be easier to read again

But in either case, you need to read the file into a matrix.
For the format you show up, you can use textscan with the
appropriate format specifier.

If the data was in the simpler form I show up, you can just
read it much easier using one command such as
importdata()

http://www.mathworks.com/help/matlab/import_export/import-numeric-data-and-header-text-from-a-text-file.html

--Nasser

Subject: text file data into matrix format

From: ali

Date: 11 Dec, 2012 05:48:08

Message: 3 of 11

"Nasser M. Abbasi" wrote in message <k94jl2$osv$1@speranza.aioe.org>...
> On 11/28/2012 2:37 AM, ali wrote:
> > hi
> >
> > i am a beginner in Matlab. i am just stuck in part of my project. i have a
> >text file which has the following format data.
> >
> >
> > x= 71, y= 7, z= 228
> >
> > x= 70, y= 5, z= 228
> >
> > x= 71, y= 5, z= 227
> >
> > x= 71, y= 6, z= 230
> >
> > x= 71, y= 6, z= 228
> >
> > x= 71, y= 7, z= 228
> >
> >
> > i want make a graph for each axis. for that i have to make a column matrix for x, y and z.
>
> There is nothing really called 'column matrix`. It is called just a matrix.
>
> >
> > i don't from where to start.
> >
> > plz any kind of code help would be a great for me.
>
> If you can change the program or function that was used to generate
> the output above so that it writes the output in simpler way,
> like this
>
> 71 7 228
> 70 5 228
>
> etc...
>
> it will be easier to read again
>
> But in either case, you need to read the file into a matrix.
> For the format you show up, you can use textscan with the
> appropriate format specifier.
>
> If the data was in the simpler form I show up, you can just
> read it much easier using one command such as
> importdata()
>
> http://www.mathworks.com/help/matlab/import_export/import-numeric-data-and-header-text-from-a-text-file.html
>
> --Nasser

Thank you nasser

as per your instruction i have written in following way:

fid = fopen('new1.txt'); % the file name is new1.txt which contains 1971X1 cell
mydata = textscan(fid, '%s %f, %s %f, %s %f');
fclose(fid);

but my data shows only for 450X1. i just can't understand why. but the data for x, y, z is extracted and showed in matrix form. but the problem is they are not showing all the data.

can you help me?

Subject: text file data into matrix format

From: Nasser M. Abbasi

Date: 11 Dec, 2012 06:41:55

Message: 4 of 11

On 12/10/2012 11:48 PM, ali wrote:

>
> as per your instruction i have written in following way:
>
> fid = fopen('new1.txt'); % the file name is new1.txt which contains 1971X1 cell
> mydata = textscan(fid, '%s %f, %s %f, %s %f');
> fclose(fid);
>
> but my data shows only for 450X1. i just can't understand why. but
>the data for x, y, z is extracted and showed in matrix form. but
>the problem is they are not showing all the data.
>

are you saying it is read only 450 lines from the file, while the
file contains 1971 lines? humm... may be you can look at
line 451 in the textfile and see if has anything weired in it.

I tried this on my matlab 2012a for 3 lines, and everything worked
ok. I do not know why it stopped at line 450. If you post your textfile
somewhere may be you'll get better help.


---foo.txt-----------
x= 71, y= 7, z= 228
  x= 70, y= 5, z= 228
  x= 71, y= 5, z= 227
----------------------

EDU>> fid = fopen('foo.txt');
EDU>> mydata = textscan(fid, '%s %f, %s %f, %s %f');
EDU>> mydata{:}

ans =
     'x='
     'x='
     'x='

ans =
     71
     70
     71

ans =
     'y='
     'y='
     'y='

ans =
      7
      5
      5

ans =
     'z='
     'z='
     'z='

ans =
    228
    228
    227


--Nasser

Subject: text file data into matrix format

From: ali

Date: 12 Dec, 2012 07:08:09

Message: 5 of 11

"Nasser M. Abbasi" wrote in message <ka6kji$1uv$1@speranza.aioe.org>...
> On 12/10/2012 11:48 PM, ali wrote:
>
> >
> > as per your instruction i have written in following way:
> >
> > fid = fopen('new1.txt'); % the file name is new1.txt which contains 1971X1 cell
> > mydata = textscan(fid, '%s %f, %s %f, %s %f');
> > fclose(fid);
> >
> > but my data shows only for 450X1. i just can't understand why. but
> >the data for x, y, z is extracted and showed in matrix form. but
> >the problem is they are not showing all the data.
> >
>
> are you saying it is read only 450 lines from the file, while the
> file contains 1971 lines? humm... may be you can look at
> line 451 in the textfile and see if has anything weired in it.
>
> I tried this on my matlab 2012a for 3 lines, and everything worked
> ok. I do not know why it stopped at line 450. If you post your textfile
> somewhere may be you'll get better help.
>
>
> ---foo.txt-----------
> x= 71, y= 7, z= 228
> x= 70, y= 5, z= 228
> x= 71, y= 5, z= 227
> ----------------------
>
> EDU>> fid = fopen('foo.txt');
> EDU>> mydata = textscan(fid, '%s %f, %s %f, %s %f');
> EDU>> mydata{:}
>
> ans =
> 'x='
> 'x='
> 'x='
>
> ans =
> 71
> 70
> 71
>
> ans =
> 'y='
> 'y='
> 'y='
>
> ans =
> 7
> 5
> 5
>
> ans =
> 'z='
> 'z='
> 'z='
>
> ans =
> 228
> 228
> 227
>
>
> --Nasser


Dear Nasser

thanx for helping me

i have uploaded the file here.
http://www.mediafire.com/view/?dd4mdib53jl8dhw

plz check the file

thank you

Subject: text file data into matrix format

From: Nasser M. Abbasi

Date: 12 Dec, 2012 07:58:27

Message: 6 of 11

On 12/12/2012 1:08 AM, ali wrote:

>
> i have uploaded the file here.
> http://www.mediafire.com/view/?dd4mdib53jl8dhw
>
> plz check the file
>

That is not how to upload a file. Just put it as .txt file,
somewhere one can download it! You just showed a listing of
it. What do you expect someone to do by looking at a listing
that goes on for 36 pages on the web?

--Nasser

Subject: text file data into matrix format

From: ali

Date: 12 Dec, 2012 15:37:08

Message: 7 of 11

"Nasser M. Abbasi" wrote in message <ka9df3$qga$1@speranza.aioe.org>...
> On 12/12/2012 1:08 AM, ali wrote:
>
> >
> > i have uploaded the file here.
> > http://www.mediafire.com/view/?dd4mdib53jl8dhw
> >
> > plz check the file
> >
>
> That is not how to upload a file. Just put it as .txt file,
> somewhere one can download it! You just showed a listing of
> it. What do you expect someone to do by looking at a listing
> that goes on for 36 pages on the web?
>
> --Nasser
Sorry Nasser
I did not the rules of uploading
I think I got the problem
After 450 lines the format of the lines become
x=-113, y= 454, z= 37

Which shows the space between = sign and the number is filled up with signs. I tried strfind but did not work

Now I want your advice about the general solution of this text file
Would you help plz

Thank you for your reply

Subject: text file data into matrix format

From: dpb

Date: 12 Dec, 2012 15:48:40

Message: 8 of 11

On 12/12/2012 9:37 AM, ali wrote:
...

> After 450 lines the format of the lines become
> x=-113, y= 454, z= 37
>
> Which shows the space between = sign and the number is filled up with
> signs. I tried strfind but did not work
>
> Now I want your advice about the general solution of this text file
...

use a format string that matches...

fmt=['x=%f, y=%f, z=%f'];

[x,y,z]=textread('yourfile.nam',fmt);

Or, textscan to return a cell...

--

Subject: text file data into matrix format

From: ali

Date: 17 Dec, 2012 07:07:26

Message: 9 of 11

dpb <none@non.net> wrote in message <kaa90e$v58$1@speranza.aioe.org>...
> On 12/12/2012 9:37 AM, ali wrote:
> ...
>
> > After 450 lines the format of the lines become
> > x=-113, y= 454, z= 37
> >
> > Which shows the space between = sign and the number is filled up with
> > signs. I tried strfind but did not work
> >
> > Now I want your advice about the general solution of this text file
> ...
>
> use a format string that matches...
>
> fmt=['x=%f, y=%f, z=%f'];
>
> [x,y,z]=textread('yourfile.nam',fmt);
>
> Or, textscan to return a cell...
>
> --

thanx for the reply

i have used the following codes


b= importdata('new.txt');
c= b(2:2:end,:);
n=strrep(c,'=',' ')
c = textscan(n, '%s %f, %s %f, %s %f')


new.txt file contains the following amount of strings:

x=-115, y=-143, z= 449

x= -70, y= -29, z= 511

x= 81, y=-137, z= 511

x= 176, y=-127, z= 448

x= 265, y=-109, z= 311

x= 322, y= -96, z= 55

x= 331, y= -91, z= 152

x= 317, y= -68, z= 41

x= 311, y= -45, z= 17

x= 321, y= -46, z= 139

x= 320, y= -71, z= 352

x= 299, y= -81, z= 498

x= 275, y= -81, z= 487

x= 260, y= -83, z= 364

x= 248, y= -92, z= 230

but the textscan is not woriking. all i want to do is separating each x, y , z values in each columns so that i could make a graph.

plz would you help.

thank you again for your help
 

Subject: text file data into matrix format

From: dpb

Date: 17 Dec, 2012 15:09:58

Message: 10 of 11

On 12/17/2012 1:07 AM, ali wrote:
> dpb <none@non.net> wrote in message <kaa90e$v58$1@speranza.aioe.org>...
...

>>
>> use a format string that matches...
>>
>> fmt=['x=%f, y=%f, z=%f'];
>> [x,y,z]=textread('yourfile.nam',fmt);
>>
...

> i have used the following codes
>
> b= importdata('new.txt');
> c= b(2:2:end,:);
> n=strrep(c,'=',' ')
> c = textscan(n, '%s %f, %s %f, %s %f')
>
>
> new.txt file contains the following amount of strings:
>
> x=-115, y=-143, z= 449
> x= -70, y= -29, z= 511
...
> x= 260, y= -83, z= 364
> x= 248, y= -92, z= 230
> but the textscan is not woriking. all i want to do is separating each x,
> y , z values in each columns so that i could make a graph.
>
> plz would you help.
...

Already did...sometimes the older tools are simpler/easier than the
newer-fangled ones...use textread() instead--

fmt=['x=%f, y=%f, z=%f'];
[x,y,z]=textread('new.txt',fmt);

If you only want the even-numbered rows, _now_ do the decimation--

x(1:2:end,:)=[]; % etc., for others

Or, of course, you can concatenate into a single array by

data=[x y z];

and then use data(1,:), etc., for x...

--

Subject: text file data into matrix format

From: ali

Date: 18 Dec, 2012 05:01:05

Message: 11 of 11

dpb <none@non.net> wrote in message <kancij$ge5$1@speranza.aioe.org>...
> On 12/17/2012 1:07 AM, ali wrote:
> > dpb <none@non.net> wrote in message <kaa90e$v58$1@speranza.aioe.org>...
> ...
>
> >>
> >> use a format string that matches...
> >>
> >> fmt=['x=%f, y=%f, z=%f'];
> >> [x,y,z]=textread('yourfile.nam',fmt);
> >>
> ...
>
> > i have used the following codes
> >
> > b= importdata('new.txt');
> > c= b(2:2:end,:);
> > n=strrep(c,'=',' ')
> > c = textscan(n, '%s %f, %s %f, %s %f')
> >
> >
> > new.txt file contains the following amount of strings:
> >
> > x=-115, y=-143, z= 449
> > x= -70, y= -29, z= 511
> ...
> > x= 260, y= -83, z= 364
> > x= 248, y= -92, z= 230
> > but the textscan is not woriking. all i want to do is separating each x,
> > y , z values in each columns so that i could make a graph.
> >
> > plz would you help.
> ...
>
> Already did...sometimes the older tools are simpler/easier than the
> newer-fangled ones...use textread() instead--
>
> fmt=['x=%f, y=%f, z=%f'];
> [x,y,z]=textread('new.txt',fmt);
>
> If you only want the even-numbered rows, _now_ do the decimation--
>
> x(1:2:end,:)=[]; % etc., for others
>
> Or, of course, you can concatenate into a single array by
>
> data=[x y z];
>
> and then use data(1,:), etc., for x...
>
> --
thanx, it works.

many many thanx

Tags for this Thread

No tags are associated with 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