Home > Archive > Matlab > April 2005 > Reading Data From Text File Storing Into Custom Named Variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Reading Data From Text File Storing Into Custom Named Variable
|
|
| Mike Kieslich 2005-04-22, 9:00 pm |
| I have been trying to import some data into MatLab for some simple
graphing. Below is a sample of my data file. The data import wizard
works all right, but I would like a better understanding of this so I
can possibly do it better.
Is it possible to take the file below and break it into 2 separate
arrays named by the title of the columns (TIME & AFR in this case)?
Basically an output of:
TIME=[0.000,0.110,0.220,0.330,...]
AFR=[3042.92,3042.92,3042.92,3042.92,...]
TIME, AFR
0.000, 3042.90,
0.110, 3042.90,
0.220, 3042.90,
0.330, 3042.90,
0.440, 3042.90,
0.550, 3042.90,
0.660, 3042.90,
| |
| Mike Kieslich 2005-04-22, 9:00 pm |
| Mike Kieslich wrote:
>
>
> I have been trying to import some data into MatLab for some simple
> graphing. Below is a sample of my data file. The data import
> wizard
> works all right, but I would like a better understanding of this so
> I
> can possibly do it better.
>
> Is it possible to take the file below and break it into 2 separate
> arrays named by the title of the columns (TIME & AFR in this case)?
>
> Basically an output of:
> TIME=[0.000,0.110,0.220,0.330,...]
> AFR=[3042.92,3042.92,3042.92,3042.92,...]
>
> TIME, AFR
> 0.000, 3042.90,
> 0.110, 3042.90,
> 0.220, 3042.90,
> 0.330, 3042.90,
> 0.440, 3042.90,
> 0.550, 3042.90,
> 0.660, 3042.90,
I forgot to mention the data file also has t lines of not needed
information before what is listed above.
I am currently looking at the following to help:
<http://www.mathworks.com/support/so...olution=1-17YYD>
| |
| Michael Robbins 2005-04-22, 9:00 pm |
| <snip>
> Is it possible to take the file below and break it into 2 separate
> arrays named by the title of the columns (TIME & AFR in this case)?
>
> Basically an output of:
> TIME=[0.000,0.110,0.220,0.330,...]
> AFR=[3042.92,3042.92,3042.92,3042.92,...]
>
> TIME, AFR
> 0.000, 3042.90,
> 0.110, 3042.90,
> 0.220, 3042.90,
> 0.330, 3042.90,
> 0.440, 3042.90,
> 0.550, 3042.90,
> 0.660, 3042.90,
There are many ways to do this. Most MATLAB coders prefer other
functions like SSCANF over REGEXP but I like the flexability of
REGEXP. You may not need it now, but as you maintain your code, you
may want the flexability.
You can probably do this whole exercise in about 6 lines instead of 9
but I don't see the point.
% SLURP THE FILE
FID = fopen('yourfile.txt','r');
A = sscanf(FID,'%c');
fclose(FID);
% EXTRACT THE NUMBERS
B=regexp(A,'\d+\.\d+');
C=str2num(char(B));
% EXTRACT THE VARIABLE NAMES
B=regexp(A,'[a-zA-Z]+');
% ASSIGN THE NUMBERS TO THE VARIABLE NAMES
for i=1:2
assignin('base',B{i},C(i:2:end+i-2));
end;
| |
| Michael Robbins 2005-04-22, 9:00 pm |
| > assignin('base',B{i},C(i:2:end+i-2));
You may want to use 'caller' instead of 'base' if you are writing a
function instead of a script.
| |
| Anders Björk 2005-04-22, 9:00 pm |
| What have you tried, post your code and you have a better chance to get
help?
On advice do not use eval as in the example use assignin instead that much
better. eval is sloppy matlab programming, if can be avoided do so...
I think the example should work for you after you have reduced it to reading
in two columms.
BR
Anders
"Mike Kieslich" <Michael.Kieslich@REMOVETHISuconn.edu> skrev i meddelandet
news:ef03b4b.0@webx.raydaftYaTP...
> Mike Kieslich wrote:
>
> I forgot to mention the data file also has t lines of not needed
> information before what is listed above.
>
> I am currently looking at the following to help:
> <http://www.mathworks.com/support/so...olution=1-17YYD>
| |
| Mike Kieslich 2005-04-22, 9:00 pm |
| Right now I have figured out how to read the file I made and put it
into the correct marticies but the code is by no means adaptive.
[names,afr,boost,hp,tq,blownup] = textread('test.txt','%s %f %d %d %d
%s','headerlines',1);
------TEST FILE------------------------------
Name AFR BOOST HP TQ Y/N
Aldo 11.20 20 500 500 Yes
Bob 10.11 10 600 600 Yes
Chris 14.01 5 0 0 No
Dan 15.58 25 2550 0 No
-----------------------------------------------
-- I appreciate the support everyone.
-Mike
| |
| Mike Kieslich 2005-04-22, 9:00 pm |
| Michael Robbins wrote:
>
>
> <snip>
>
> There are many ways to do this. Most MATLAB coders prefer other
> functions like SSCANF over REGEXP but I like the flexability of
> REGEXP. You may not need it now, but as you maintain your code,
> you
> may want the flexability.
>
> You can probably do this whole exercise in about 6 lines instead of
> 9
> but I don't see the point.
>
> % SLURP THE FILE
> FID = fopen('yourfile.txt','r');
> A = sscanf(FID,'%c');
> fclose(FID);
>
> % EXTRACT THE NUMBERS
> B=regexp(A,'\d+\.\d+');
> C=str2num(char(B));
>
> % EXTRACT THE VARIABLE NAMES
> B=regexp(A,'[a-zA-Z]+');
>
> % ASSIGN THE NUMBERS TO THE VARIABLE NAMES
> for i=1:2
> assignin('base',B{i},C(i:2:end+i-2));
> end;
Michael,
When trying your code I only make it to the second line. I am using
the same sample file as posted originally. I had tried the fopen
command before, but I dont understand it too well. It is returning a
single value (1x1) array with a 5 in it.
[color=darkred]
??? Error using ==> sscanf
First argument must be a string.
The fopen help info says it should be a "is a scalar MATLAB integer,
called a file identifier." What does the FID do exactly?
Thanks,
Mike
| |
| Michael Robbins 2005-04-22, 9:00 pm |
| <snip>
> ??? Error using ==> sscanf
> First argument must be a string.
Change sscanf to fscanf
| |
| Mike Kieslich 2005-04-22, 9:00 pm |
| Michael Robbins wrote:
>
>
> <snip>
>
>Change sscanf to fscanf
Michael,
I Still get an error.
A = fscanf(FID,'%c');
??? Error using ==> fscanf
Invalid fid.
| |
| Michael Robbins 2005-04-22, 9:00 pm |
| Mike Kieslich wrote:
>
>
> Michael Robbins wrote:
>
> Michael,
> I Still get an error.
> A = fscanf(FID,'%c');
> ??? Error using ==> fscanf
> Invalid fid.
What is the value of FID?
HELP FOPEN
| |
| Mike Kieslich 2005-04-22, 9:00 pm |
| Michael Robbins wrote:
>
>
> Mike Kieslich wrote:
>
> What is the value of FID?
>
> HELP FOPEN
FID=4
| |
| Michael Robbins 2005-04-22, 9:00 pm |
| >> What is the value of FID?
>
> FID=4
:(
What happens when you do this:
[message,errnum] = ferror(FID)
| |
| Mike Kieslich 2005-04-22, 9:00 pm |
| Michael Robbins wrote:
>
>
>
> :(
>
> What happens when you do this:
>
> [message,errnum] = ferror(FID)
This is strange. I just ran solved for FID again and got 5.
[color=darkred]
message =
''
errnum =
0
| |
| Michael Robbins 2005-04-22, 9:00 pm |
| What is the file name you are using in the FOPEN statement. Does the
file exist? Try this:
!dir filename
!type filename
but replace filename, with the name of your file.
| |
| Mike Kieslich 2005-04-22, 9:00 pm |
| Michael Robbins wrote:
>
>
> What is the file name you are using in the FOPEN statement. Does
> the
> file exist? Try this:
>
> !dir filename
> !type filename
>
> but replace filename, with the name of your file.
[color=darkred]
Volume in drive C has no label.
Volume Serial Number is 2C8E-C659
Directory of C:\Documents and Settings\TEMP.ENGR_STUDENT.000\Desktop
04/22/2005 05:33 PM 128 test.txt
1 File(s) 128 bytes
0 Dir(s) 62,316,568,576 bytes free[color=darkred]
TIME, AFR
0.000, 3042.90
0.110, 3042.90
0.220, 3042.90
0.330, 3042.90
0.440, 3042.90
0.550, 3042.90
0.660, 3042.90[color=darkred]
| |
| Michael Robbins 2005-04-23, 3:59 am |
| >>> !type test.txt
> TIME, AFR
> 0.000, 3042.90
> 0.110, 3042.90
> 0.220, 3042.90
> 0.330, 3042.90
> 0.440, 3042.90
> 0.550, 3042.90
> 0.660, 3042.90
Hmmmmmmmmmmmm...
And
fclose('all');
FID = fopen('test.txt','r');
x = fscanf(FID,'%c');
fclose(FID);
doesn't work?
What about
fclose('all');
FID = fopen('test.txt','rt');
x = fscanf(FID,'%c');
fclose(FID);
Is that any better?
| |
| Mike Kieslich 2005-04-23, 3:59 am |
| <snip>
> fclose('all');
> FID = fopen('test.txt','rt');
> x = fscanf(FID,'%c');
> fclose(FID);
>
> Is that any better?
I got through that code just fine! x is an array of the exact layout
of the file. I then tried the extract numbers portion of the code.
I got a result, but it does not contain any numbers that I orig had
in the file.
I ran
% EXTRACT THE NUMBERS
B=regexp(x,'\d+\.\d+');
C=str2num(char(B));
C is empty and B is a 1x14 matrix with the following inside:
12 19 28 35 44 51 60 67 76 83 92 99 108
115
| |
| Michael Robbins 2005-04-23, 3:59 am |
| <snip>
> B=regexp(x,'\d+\.\d+');
I'm terribly sorry, I'm doing this without MATLAB and I have to guess
at the syntax. The above line should be
B=regexp(x,'\d+\.\d+','match');
So the whole thing, is as follows.
By the way, what was the problem with the file read?
%% CODE FOLLOWS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SLURP THE FILE
FID = fopen('yourfile.txt','r');
A = sscanf(FID,'%c');
fclose(FID);
% EXTRACT THE NUMBERS
B=regexp(A,'\d+\.\d+','match');
C=str2num(char(B));
% EXTRACT THE VARIABLE NAMES
B=regexp(A,'[a-zA-Z]+');
% ASSIGN THE NUMBERS TO THE VARIABLE NAMES
for i=1:2
assignin('base',B{i},C(i:2:end+i-2));
end;
| |
| Michael Robbins 2005-04-23, 3:59 am |
| UUUUURRRRGGGGGHHHHH!
I pasted the code that had SSCANF, here's another try:
% SLURP THE FILE
FID = fopen('yourfile.txt','r');
A = fscanf(FID,'%c');
fclose(FID);
% EXTRACT THE NUMBERS
B=regexp(A,'\d+\.\d+','match');
C=str2num(char(B));
% EXTRACT THE VARIABLE NAMES
B=regexp(A,'[a-zA-Z]+');
% ASSIGN THE NUMBERS TO THE VARIABLE NAMES
for i=1:2
assignin('base',B{i},C(i:2:end+i-2));
end;
| |
| Mike Kieslich 2005-04-23, 3:59 am |
| Michael Robbins wrote:
>
>
> ... and of course, the second match. I'm going to sleep after I
> hit
> send:
>
> % SLURP THE FILE
> FID = fopen('yourfile.txt','r');
> A = fscanf(FID,'%c');
> fclose(FID);
>
> % EXTRACT THE NUMBERS
> B=regexp(A,'\d+\.\d+','match');
> C=str2num(char(B));
>
> % EXTRACT THE VARIABLE NAMES
> B=regexp(A,'[a-zA-Z]+','match');
>
> % ASSIGN THE NUMBERS TO THE VARIABLE NAMES
> for i=1:2
> assignin('base',B{i},C(i:2:end+i-2));
> end;
Michael,
Thank you sooo much for all your help. I really have no idea what
was up with the data file. I hope to figure that out soon. I have
not tried the new code yet. I will post results in a few minutes.
--Mike
| |
|
| Mike Kieslich wrote:
>
>
> I have been trying to import some data into MatLab for some simple
> graphing. Below is a sample of my data file. The data import
> wizard
> works all right, but I would like a better understanding of this so
> I
> can possibly do it better.
>
> Is it possible to take the file below and break it into 2 separate
> arrays named by the title of the columns (TIME & AFR in this case)?
>
> Basically an output of:
> TIME=[0.000,0.110,0.220,0.330,...]
> AFR=[3042.92,3042.92,3042.92,3042.92,...]
>
> TIME, AFR
> 0.000, 3042.90
> 0.110, 3042.90
> 0.220, 3042.90
> 0.330, 3042.90
> 0.440, 3042.90
> 0.550, 3042.90
> 0.660, 3042.90
Mike,
Autodataread (in File Central) will read this data and assign it as a
structure array of column vectors with the header as variable name.
It also has a parameter association block so you can interpret the
column headings any way you please.
hth,
Scott
|
|
|
|
|