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:
4 questions dealing with text2mat function ?

Subject: 4 questions dealing with text2mat function ?

From: Philippe MOÇOTÉGUY

Date: 1 Apr, 2010 08:38:05

Message: 1 of 10

Dear all,

I developed a Matlab program to convert experimental ascii raw data into data treatable by Matlab, by using the txt2mat function.

I have 4 different questions:

First one:

when I uses the following options for this function:
t2mOpts = {'ReplaceChar',{',.'}};
A = txt2mat(datafile, t2mOpts{:});
Even if function runs properly and finds the 2 header lines and the main data, it is not able to identify properly date and time of experiments.
The matrix to be imported looks like this in the Windows Notebook (actual is much bigger):
Date;Time;Sim time;Voltage;
;;;V;
11.02.2010;09:59:03;00:00:00;36,352;
11.02.2010;09:59:04;00:00:00;36,352;
11.02.2010;09:59:05;00:00:00;36,372;
11.02.2010;09:59:06;00:00:00;36,372;
The "A" output matrix is
11,020000 0,20100000 9 59 3 0 0 0 36.352
11,020000 0,20100000 9 59 4 0 0 0 36.352
11,020000 0,20100000 9 59 5 0 0 0 36.372
11,020000 0,20100000 9 59 6 0 0 0 36.372

How can I get the correct time (at least: in seconds vs midnight) and date values in treatable form ?

Second question:

As mentioned, the number of header line is correct, but I can't get them.
Then I used this command, recommended by txt2mat function's help: [A,ffn,nh,SR,hl,fpos] = txt2mat(datafile, t2mOpts{:});
The output for "A" is the same and the ouput for "hl", normaly the variable containing my headers is a single chain of caracters separated by semicolumn, in which the 2 header lines are concatenated line by line:
Date;Time;Sim time;Voltage;;;;V;

How can get this "hl" value in the form of a 2*4 matrix of cells of characters, like this:
Date Time Sim time Voltage
                                            V

Third question:
How can I replace this command:
[A,ffn,nh,SR,hl,fpos] = txt2mat(datafile, t2mOpts{:});
by an another one which output would only be "A" and "hl" (in the format mentioned in 2nd question, without generating a Matlab error ?

Fourth question:
How can I compress the "A" matrix into a smaller one that exclude all columns that only contain 0 values and get the corresponding headers ? As least, without checking with a loop, column by column and then cell by cell, that this cell's value is not 0 for keeping this column !...

Thanks by advance to all and best regards.

Subject: 4 questions dealing with text2mat function ?

From: us

Date: 1 Apr, 2010 11:15:05

Message: 2 of 10

"Philippe MOÇOTÉGUY" <philippe.mocoteguy@eifer.uni-karlsruhe.de> wrote in message <hp1m1d$t3i$1@fred.mathworks.com>...
> Dear all,
>
> I developed a Matlab program to convert experimental ascii raw data into data treatable by Matlab, by using the txt2mat function.
>
> I have 4 different questions:
>
> First one:
>
> when I uses the following options for this function:
> t2mOpts = {'ReplaceChar',{',.'}};
> A = txt2mat(datafile, t2mOpts{:});
> Even if function runs properly and finds the 2 header lines and the main data, it is not able to identify properly date and time of experiments.
> The matrix to be imported looks like this in the Windows Notebook (actual is much bigger):
> Date;Time;Sim time;Voltage;
> ;;;V;
> 11.02.2010;09:59:03;00:00:00;36,352;
> 11.02.2010;09:59:04;00:00:00;36,352;
> 11.02.2010;09:59:05;00:00:00;36,372;
> 11.02.2010;09:59:06;00:00:00;36,372;
> The "A" output matrix is
> 11,020000 0,20100000 9 59 3 0 0 0 36.352
> 11,020000 0,20100000 9 59 4 0 0 0 36.352
> 11,020000 0,20100000 9 59 5 0 0 0 36.372
> 11,020000 0,20100000 9 59 6 0 0 0 36.372
>
> How can I get the correct time (at least: in seconds vs midnight) and date values in treatable form ?
>
> Second question:
>
> As mentioned, the number of header line is correct, but I can't get them.
> Then I used this command, recommended by txt2mat function's help: [A,ffn,nh,SR,hl,fpos] = txt2mat(datafile, t2mOpts{:});
> The output for "A" is the same and the ouput for "hl", normaly the variable containing my headers is a single chain of caracters separated by semicolumn, in which the 2 header lines are concatenated line by line:
> Date;Time;Sim time;Voltage;;;;V;
>
> How can get this "hl" value in the form of a 2*4 matrix of cells of characters, like this:
> Date Time Sim time Voltage
> V
>
> Third question:
> How can I replace this command:
> [A,ffn,nh,SR,hl,fpos] = txt2mat(datafile, t2mOpts{:});
> by an another one which output would only be "A" and "hl" (in the format mentioned in 2nd question, without generating a Matlab error ?
>
> Fourth question:
> How can I compress the "A" matrix into a smaller one that exclude all columns that only contain 0 values and get the corresponding headers ? As least, without checking with a loop, column by column and then cell by cell, that this cell's value is not 0 for keeping this column !...
>
> Thanks by advance to all and best regards.

one of the solutions
- often its better to slurp the file and then do some processing...

% the data
     fnam='ng123.txt';
% the engine
     s=textread(fnam,'%s','delimiter','\n');
     s=s(3:end); % <- remove header
     s=strrep(s,',','.');
     r=regexp(s,';','split');
     r=reshape([r{:}].',5,[]).';
     r(:,end)=[];
     rtim=datevec(r(:,2));
     rtim=rtim(:,4:end);
     rvol=cellfun(@(x) sscanf(x,'%f'),r(:,4),'uni',false);
     rvol=cat(1,rvol{:});
% the result
     disp(r);
     disp(rtim);
     disp(rvol);
%{
% R =
     '11.02.2010' '09:59:03' '00:00:00' '36.352'
     '11.02.2010' '09:59:04' '00:00:00' '36.352'
     '11.02.2010' '09:59:05' '00:00:00' '36.372'
     '11.02.2010' '09:59:06' '00:00:00' '36.372'
% time: H M S
     9 59 3
     9 59 4
     9 59 5
     9 59 6
% voltage
     36.352
     36.352
     36.372
     36.372
%}

us

Subject: 4 questions dealing with text2mat function ?

From: Andres

Date: 1 Apr, 2010 13:55:06

Message: 3 of 10

Hi Philippe,

hopefully this code contains the answers to all your questions:

>>
filename = 'C:\temp\philippem.txt';

% one of the possible solutions taken from EXAMPLE 4 in txt2mat's doc
[A,~,~,~,hl] = txt2mat(filename,'ReplaceRegExpr',{{'\.(\d+)\.',' $1 '}});
A = A(:,any(A~=0,1));

% yields
%{
hl =
Date;Time;Sim time;Voltage;
;;;V;

A =
11 2 2010 9 59 3 36.352
11 2 2010 9 59 4 36.352
11 2 2010 9 59 5 36.372
11 2 2010 9 59 6 36.372
%}
<<

I can't reproduce your hl == A behaviour. Have you taken the latest version from the file exchange?
If you have questions regarding a fex submission, you may also use the 'Contact Author' link.
Best wishes
Andres

Subject: 4 questions dealing with text2mat function ?

From: Andres

Date: 1 Apr, 2010 14:05:29

Message: 4 of 10

"Philippe MOÇOTÉGUY" <philippe.mocoteguy@eifer.uni-karlsruhe.de> wrote in message <hp1m1d$t3i$1@fred.mathworks.com>...
> [...]
> The output for "A" is the same and the ouput for "hl", normaly the variable containing my headers is a single chain of caracters separated by semicolumn, in which the 2 header lines are concatenated line by line:
> Date;Time;Sim time;Voltage;;;;V;
>
> How can get this "hl" value in the form of a 2*4 matrix of cells of characters, like this:
> Date Time Sim time Voltage
> V

Sorry, I misread this! txt2mat returns the whole header as a string (note there's a line break in it). You'll have to split it into separate cells manually - e.g. take us's code as an (condensed) example.

Subject: 4 questions dealing with text2mat function ?

From: Andres

Date: 1 Apr, 2010 20:19:20

Message: 5 of 10

a complete answer:

% load the file
filename = 'C:\temp\philippem.txt';
[A,nh,nh,hl,hl] = txt2mat(filename,'ReplaceRegExpr',{{'\.(\d+)\.',' $1 '}});

% remove zero-only columns
A = A(:,any(A~=0,1));

% get header line items delimited by ';'
kSplit = [0,strfind(hl,';')];
hItem = arrayfun(@(m,n) {hl(m:n)}, kSplit(1:end-1)+1,kSplit(2:end)-1);
hItem = reshape(hItem,numel(hItem)/nh,nh).';
hItem = strtrim(hItem);

Subject: 4 questions dealing with text2mat function ?

From: Philippe MOÇOTÉGUY

Date: 6 Apr, 2010 12:26:04

Message: 6 of 10

"Andres" <rantore@werb.deNoRs> wrote in message <hp28jq$d02$1@fred.mathworks.com>...
> Hi Philippe,

Hello
Thanks for your answer
 
> hopefully this code contains the answers to all your questions:
>
> >>
> filename = 'C:\temp\philippem.txt';
>
> % one of the possible solutions taken from EXAMPLE 4 in txt2mat's doc

With your advice, I partly solved my problem by this command:
t2mOpts = {'ReplaceChar',{'. ',': ',',.'}};
A = txt2mat(datafile, t2mOpts{:});
The output is what I wanted, at least for data. But still no way to recover the headers

> [A,~,~,~,hl] = txt2mat(filename,'ReplaceRegExpr',{{'\.(\d+)\.',' $1 '}});

But the above expression returns me the following error message:
"Error: Expression or statement is incorrect--possibly unbalanced (, {, or
[."
By the way, could you explain me the arguments you put in txt2mat function ? I coudn't manage to understand them from the help.

> A = A(:,any(A~=0,1));

This works perfectly: thanks! The A output you mentioned is what I got...
But I still need to get corresponding headers.

> % yields
> %{
> hl =
> Date;Time;Sim time;Voltage;
> ;;;V;

> A =
> 11 2 2010 9 59 3 36.352
> 11 2 2010 9 59 4 36.352
> 11 2 2010 9 59 5 36.372
> 11 2 2010 9 59 6 36.372
> %}
> <<
>
> I can't reproduce your hl == A behaviour. Have you taken the latest version from the file exchange?

What do you mean by "the latest version from the file exchange"? Matlab's version ? (Mine is 2008b on my laptop and 2009a on our network)

> If you have questions regarding a fex submission, you may also use the 'Contact Author' link.
> Best wishes
> Andres

Thanks and best regards

Subject: 4 questions dealing with text2mat function ?

From: Philippe MOÇOTÉGUY

Date: 6 Apr, 2010 12:26:04

Message: 7 of 10

"Andres" <rantore@werb.deNoRs> wrote in message <hp28jq$d02$1@fred.mathworks.com>...
> Hi Philippe,

Hello
Thanks for your answer
 
> hopefully this code contains the answers to all your questions:
>
> >>
> filename = 'C:\temp\philippem.txt';
>
> % one of the possible solutions taken from EXAMPLE 4 in txt2mat's doc

With your advice, I partly solved my problem by this command:
t2mOpts = {'ReplaceChar',{'. ',': ',',.'}};
A = txt2mat(datafile, t2mOpts{:});
The output is what I wanted, at least for data. But still no way to recover the headers

> [A,~,~,~,hl] = txt2mat(filename,'ReplaceRegExpr',{{'\.(\d+)\.',' $1 '}});

But the above expression returns me the following error message:
"Error: Expression or statement is incorrect--possibly unbalanced (, {, or
[."
By the way, could you explain me the arguments you put in txt2mat function ? I coudn't manage to understand them from the help.

> A = A(:,any(A~=0,1));

This works perfectly: thanks! The A output you mentioned is what I got...
But I still need to get corresponding headers.

> % yields
> %{
> hl =
> Date;Time;Sim time;Voltage;
> ;;;V;

> A =
> 11 2 2010 9 59 3 36.352
> 11 2 2010 9 59 4 36.352
> 11 2 2010 9 59 5 36.372
> 11 2 2010 9 59 6 36.372
> %}
> <<
>
> I can't reproduce your hl == A behaviour. Have you taken the latest version from the file exchange?

What do you mean by "the latest version from the file exchange"? Matlab's version ? (Mine is 2008b on my laptop and 2009a on our network)

> If you have questions regarding a fex submission, you may also use the 'Contact Author' link.
> Best wishes
> Andres

Thanks and best regards

Subject: 4 questions dealing with text2mat function ?

From: Andres

Date: 6 Apr, 2010 14:09:05

Message: 8 of 10

"Philippe MOÇOTÉGUY" <philippe.mocoteguy@eifer.uni-karlsruhe.de> wrote in message <hpf98s$7o8$1@fred.mathworks.com>...
> With your advice, I partly solved my problem by this command:
> t2mOpts = {'ReplaceChar',{'. ',': ',',.'}};
> A = txt2mat(datafile, t2mOpts{:});

Ok, these replacements work as well since the '.' is uniquely used in the file as the date separator and _not_ as decimal character.


> The output is what I wanted, at least for data. But still no way to recover the headers
>
> > [A,~,~,~,hl] = txt2mat(filename,'ReplaceRegExpr',{{'\.(\d+)\.',' $1 '}});
>
> But the above expression returns me the following error message:
> "Error: Expression or statement is incorrect--possibly unbalanced (, {, or
> [."

Try
[A,nh,nh,hl,hl] = txt2mat(...
instead -- probably your version of matlab does not support the '~' output argument.


> By the way, could you explain me the arguments you put in txt2mat function ? I coudn't manage to understand them from the help.

I wrote
[A,nh,nh,hl,hl] = txt2mat(filename,'ReplaceRegExpr',{{'\.(\d+)\.',' $1 '}});
This uses a regular expression replacement which replaces two dots surrounding a sequence of digits (as appearing in the date string) by spaces, e.g. '.02.' -> ' 02 ', while it leaves a single dot possibly representing the decimal character untouched. Type
doc regexp
if you want to learn more about regular expressions.

 
> > A = A(:,any(A~=0,1));
>
> This works perfectly: thanks! The A output you mentioned is what I got...
> But I still need to get corresponding headers.

Please try [A,nh,nh,hl,hl] = txt2mat(... and then the code I already posted:

kSplit = [0,strfind(hl,';')];
hItem = arrayfun(@(m,n) {hl(m:n)}, kSplit(1:end-1)+1,kSplit(2:end)-1);
hItem = reshape(hItem,numel(hItem)/nh,nh).';
hItem = strtrim(hItem);


> What do you mean by "the latest version from the file exchange"? Matlab's version ? (Mine is 2008b on my laptop and 2009a on our network)

txt2mat ist not included within Matlab, but it is hosted on the mathworks file exchange: http://www.mathworks.com/matlabcentral/fileexchange/18430-txt2mat . Authors of a file exchange submission may upload updated code -- but most likely this wasn't the problem here, I was just misreading your post at first.

Good luck
Andres

Subject: 4 questions dealing with text2mat function ?

From: Philippe MOÇOTÉGUY

Date: 7 Apr, 2010 08:02:24

Message: 9 of 10

Dear all,

With the help of all contributors, I finally solved my problem, with the following sequence:

t2mOpts = {'ReplaceChar',{'. ',': ',',.'}};
[data,nh,nh,hl,hl] = txt2mat(datafile, t2mOpts{:});
dayexp=datenum(data(:,3),data(:,2),data(:,1))-datenum(data(1,3),data(1,2),data(1,1));
time=(24*3600*dayexp)+(3600*(data(:,4)-data(1,4)))+(60*(data(:,5)-data(1,5)))+data(:,6)-data(1,6);
Charcellimits=[0,strfind(hl,';')];
Charcells = arrayfun(@(m,n) {hl(m:n)}, Charcellimits(1:end-1)+1,Charcellimits(2:end)-1);
Headertable=reshape(Charcells,(numel(Charcells)/nh),nh).';
Headertable = strtrim(Headertable);
data=data(:,(7:end));
datatable=data(:,any(data~=0,1));
headers=Headertable(:,any(data~=0,1));
headers=[{'time';'s'},headers];
Measurements=[time,datatable];

Thanks to all for your help and contributions.
Best regards.

Subject: 4 questions dealing with text2mat function ?

From: Philippe MOÇOTÉGUY

Date: 7 Apr, 2010 08:19:24

Message: 10 of 10

Dear all,

With the help of all contributors, I finally solved my problem, with the following sequence:

t2mOpts = {'ReplaceChar',{'. ',': ',',.'}};
[data,nh,nh,hl,hl] = txt2mat(datafile, t2mOpts{:});
dayexp=datenum(data(:,3),data(:,2),data(:,1))-datenum(data(1,3),data(1,2),data(1,1));
time=(24*3600*dayexp)+(3600*(data(:,4)-data(1,4)))+(60*(data(:,5)-data(1,5)))+data(:,6)-data(1,6);
Charcellimits=[0,strfind(hl,';')];
Charcells = arrayfun(@(m,n) {hl(m:n)}, Charcellimits(1:end-1)+1,Charcellimits(2:end)-1);
Headertable=reshape(Charcells,(numel(Charcells)/nh),nh).';
Headertable = strtrim(Headertable);
data=data(:,(7:end));
datatable=data(:,any(data~=0,1));
headers=Headertable(:,any(data~=0,1));
headers=[{'time';'s'},headers];
Measurements=[time,datatable];

Thanks to all for your help and contributions.
Best regards.

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