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:
using textread

Subject: using textread

From: edward kabanyas

Date: 2 May, 2012 03:21:07

Message: 1 of 4

Hi all

I read the data containing string as follow:

[Par1]= textread(fname1, '%*s %f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f');
 
if I need more parameters, for example Par1, Par2, Par3, I can read as:

[Par1 Par2 Par3]= textread(fname1, '%*s %f %f %f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f');

Is it not possible to have an output as an array assumed as Par (row x coulumn) consisting Par1, Par2, and Par3 instead of using [Par1 Par2 Par3] because if I read all parameters I must read [Par1, Par2...Par31]. Thus, I need read the file but the output is [Par(:,n)] to reduce the parameter name.

Thank you,

Edward

Subject: using textread

From: TideMan

Date: 2 May, 2012 04:15:47

Message: 2 of 4

On Wednesday, May 2, 2012 3:21:07 PM UTC+12, edward kabanyas wrote:
> Hi all
>
> I read the data containing string as follow:
>
> [Par1]= textread(fname1, '%*s %f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f');
>
> if I need more parameters, for example Par1, Par2, Par3, I can read as:
>
> [Par1 Par2 Par3]= textread(fname1, '%*s %f %f %f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f');
>
> Is it not possible to have an output as an array assumed as Par (row x coulumn) consisting Par1, Par2, and Par3 instead of using [Par1 Par2 Par3] because if I read all parameters I must read [Par1, Par2...Par31]. Thus, I need read the file but the output is [Par(:,n)] to reduce the parameter name.
>
> Thank you,
>
> Edward

Use textscan instead:
fid=fopen(fname1,'rt');
c=textscan(fid,'[%s repmat('%f',1,ncols)]);
fclose(fid);

Now c is a cell array with the columns in individual cells.

Note: it's a BAD idea to use variable names like Par1, Par2, Par3 because later on you'll have trouble accessing them. It's better to either use the cell array c from textscan, or put them in a structure s.Par1, s.Par2, s.Par2, etc.

Subject: using textread

From: edward kabanyas

Date: 2 May, 2012 08:17:06

Message: 3 of 4

Tideman;

Thanks for your reply.

 Use textscan instead:
> fid=fopen(fname1,'rt');
> c=textscan(fid,'[%s repmat('%f',1,ncols)]);
> fclose(fid);

Great, it works very well. Thanks again.

Edward

TideMan <mulgor@gmail.com> wrote in message <3036355.300.1335932147197.JavaMail.geo-discussion-forums@pbbof7>...
> On Wednesday, May 2, 2012 3:21:07 PM UTC+12, edward kabanyas wrote:
> > Hi all
> >
> > I read the data containing string as follow:
> >
> > [Par1]= textread(fname1, '%*s %f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f');
> >
> > if I need more parameters, for example Par1, Par2, Par3, I can read as:
> >
> > [Par1 Par2 Par3]= textread(fname1, '%*s %f %f %f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f');
> >
> > Is it not possible to have an output as an array assumed as Par (row x coulumn) consisting Par1, Par2, and Par3 instead of using [Par1 Par2 Par3] because if I read all parameters I must read [Par1, Par2...Par31]. Thus, I need read the file but the output is [Par(:,n)] to reduce the parameter name.
> >
> > Thank you,
> >
> > Edward
>
> Use textscan instead:
> fid=fopen(fname1,'rt');
> c=textscan(fid,'[%s repmat('%f',1,ncols)]);
> fclose(fid);
>
> Now c is a cell array with the columns in individual cells.
>
> Note: it's a BAD idea to use variable names like Par1, Par2, Par3 because later on you'll have trouble accessing them. It's better to either use the cell array c from textscan, or put them in a structure s.Par1, s.Par2, s.Par2, etc.

Subject: using textread

From: edward kabanyas

Date: 3 May, 2012 06:36:07

Message: 4 of 4

Dear Tideman;

> Use textscan instead:
> fid=fopen(fname1,'rt');
> c=textscan(fid,'[%s repmat('%f',1,ncols)]);
> fclose(fid);
>
> Now c is a cell array with the columns in individual cells.
>
> Note: it's a BAD idea to use variable names like Par1, Par2, Par3 because later on you'll >have trouble accessing them. It's better to either use the cell array c from textscan, or >put them in a structure s.Par1, s.Par2, s.Par2, etc.

I found that your texscan only possible if the data structure is uniform. In my case, some lines of the data is only white space, others sometimes with minus (-). thus the data structure is not uniform. The following is an example for several lines:



N00
N01
N02
N03
N04-1.7e+6 357851
N05-1.3e+6-3.5e+6-4.3e+6-4.7e+6-3.6e+6-3.1e+6-1.0e+6 1.9e+6-3.2e+6-6.8e+6 3.1e+6 1.2e+7 5.7e+6 -20791 7.9e+6 215300 1.3e+6 2.9e+6 1.5e+7 2.1e+7 1.2e+6-1.5e+6 1.7e+7 2.9e+7 3.3e+7 2.0e+7 1.1e+7-1.8e+7 3.3e+7 1.0e+8 2.2e+8
N06 130870-1.4e+6-2.7e+6-681635 963855-1.5e+6-2.6e+6-1.4e+6-1.6e+6-1.5e+6 5.0e+6 5.9e+6 3.8e+6 4.4e+6 7.7e+6 106980-6.2e+6 7.5e+6 1.2e+7 1.3e+7 7.0e+6 6.7e+6 1.3e+7 1.5e+7 2.3e+7-3.6e+6-1.0e+7 9.0e+6 5.7e+6 2.3e+7 1.3e+8
N07 313033-109167-453076 913430 1.9e+6-382771-249068-3831.2 4898.4 178454 1.9e+6 2.2e+6 991898 2.9e+6 4.1e+6-727086-6.1e+6 3.0e+6 9.3e+6 1.2e+7 305987-2.4e+6 2.3e+6 2.4e+6 1.2e+7-1.9e+6-5.3e+6 9.3e+6 1.9e+7 3.3e+7 4.5e+7
N08 101343-216054-162576 576081 729527 309968 1.0e+6 523168-296238-765905 630115 1.5e+6 274207 637765 1.9e+6-1.3e+6-4.4e+6 1.0e+6 5.0e+6 4.7e+6 2.0e+6 846598-1.3e+6 1.3e+6 3.2e+6 2.9e+6 1.5e+6-2.7e+6 7.8e+6 1.5e+7 3.2e+7

when I use your code to read the above data.

c{1} =
'N00'
'N01'
'N02'
'N03'
'N04-1.7e+6'
'N05-1.3e+6-3.5e+6-4.3e+6-4.7e+6-3.6e+6-3.1e+6-1.0e+6'
'N06'
'N07'
'N08'

The correct output for c{1} =
'N00'
'N01'
'N02'
'N03'
'N04'
'N05'
'N06'
'N07'
'N08'

if I read using my previous code, it works well but I must write Par1, Par2..Parn. Probably you have another suggestion...

Best regards,
Edward

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