Got Questions? Get Answers.
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:
load column in from text file

Subject: load column in from text file

From: Fiboehh

Date: 30 Nov, 2012 14:56:08

Message: 1 of 13

Hello I have the following problem:
I have 200 .txt files with the following structure:
0
0
2
3
...
It are kind of random numbers under each other, without a fixed amount. Now I need to do a lot of calculations with these data, but file after file: sequentiel. The results of the calculations have to come in an array, so I can do statistically analysis with the results.
So I read everytime a .txt file and load them in, there the problem occurs. I can't give the data a name to use in the calculations. The loaded in numbers are stored (automatic) in a variable X... which i can't achieve for my calculations.

My code I made by the MATLAB help:
close all
fnames = dir('path\*.txt')
for k = 1 : length(fnames) % all files dir read after each other
    fname = fnames(k).name;
    eval(['load ' fnames(k).name ' -ascii']);
    // Calculations -> B (need the data from the .txt)
   //
    result(k) = B;
    save('Results',result);
end

Subject: load column in from text file

From: Nasser M. Abbasi

Date: 30 Nov, 2012 15:06:52

Message: 2 of 13

On 11/30/2012 8:56 AM, Fiboehh wrote:

> eval(['load ' fnames(k).name ' -ascii']);

what is the deal with this eval thing? why not write normal
code and just call the function load() directly?

Any reason you must do it this way?

As for your question,

>> I can't give the data a name to use in the calculations.

may be you can simply use importdata?

A=importdata('myfile.txt');
or A=load(.....)

and here is your variable.
  
For text files better to use textscan or importdata I would think.

--Nasser

Subject: load column in from text file

From: Fiboehh

Date: 30 Nov, 2012 15:23:08

Message: 3 of 13

"Nasser M. Abbasi" <nma@12000.org> wrote in message <k9ai2f$3ij$1@speranza.aioe.org>...
> On 11/30/2012 8:56 AM, Fiboehh wrote:
>
> > eval(['load ' fnames(k).name ' -ascii']);
>
> what is the deal with this eval thing? why not write normal
> code and just call the function load() directly?
>
> Any reason you must do it this way?
>
> As for your question,
>
> >> I can't give the data a name to use in the calculations.
>
> may be you can simply use importdata?
>
> A=importdata('myfile.txt');
> or A=load(.....)
>
> and here is your variable.
>
> For text files better to use textscan or importdata I would think.
>
> --Nasser
>

Thanx for the reply Nasser,
The solutions you gave, work for only one 'direct' path file.
I need to do this for all my files, so I 'm not planning to change 200 times the 'myfile.txt' name.
The eval(['load ' fnames(k).name ' -ascii']); code I use to give the correct file (which is a string (converts number to string)), because the code below can't recognize the files correct.

for k = 1 : length(fnames)
    fname = fnames(k).name;
    log = impordata('fname.txt'); // fname is variable so this gives a problem
    disp(log)
end

I hope you can help me.

Subject: load column in from text file

From: dpb

Date: 30 Nov, 2012 15:31:02

Message: 4 of 13

On 11/30/2012 8:56 AM, Fiboehh wrote:
...

> Now I need to do a lot of calculations with these data, but file after
> file: sequentiel. The results of the calculations have to come in an
> array, so I can do statistically analysis with the results.

The result of processing on the individual files is to be saved across
files? What is the size of the result if so--a single value, a vector,
an array...? It is the same size each time?

> So I read everytime a .txt file and load them in, there the problem
> occurs. I can't give the data a name to use in the calculations. The
> loaded in numbers are stored (automatic) in a variable X... which i
> can't achieve for my calculations.

Only if you do it that way...

> My code I made by the MATLAB help:
> close all
> fnames = dir('path\*.txt')
> for k = 1 : length(fnames) % all files dir read after each other
> fname = fnames(k).name;
> eval(['load ' fnames(k).name ' -ascii']);
> // Calculations -> B (need the data from the .txt)
> //
> result(k) = B;
> save('Results',result);
> end

d = dir('path\*.txt'); % the result of dir() is _not_ a fname but a
directory structure; make the name more representative

% preallocate your final result--I'm assuming B is no more than a vector
here--if it's another array a cell array may be more suitable...
BigAns=zeros(length(d),length(B));

for k = 1:length(d)
   x=importdata(d(k).name); % pull the data into an array
   B = your_computation(x);
   BigAns(k) = B; % you can, of course, dispense w the intermediary
end
save results.txt BigAns -ASCII

% the above assumes you're saving across the files instead of a single
file for each of the input files. As you wrote it, you have overwritten
the results and end up w/ only the last case after all that effort.

--

Subject: load column in from text file

From: Nasser M. Abbasi

Date: 30 Nov, 2012 15:36:27

Message: 5 of 13

On 11/30/2012 9:23 AM, Fiboehh wrote:


> The solutions you gave, work for only one 'direct' path file.

I do not understand. You will use the same file names you used
in the load command, which worked.

> I need to do this for all my files, so I 'm not planning to change 200 times the
>'myfile.txt' name.
> The eval(['load ' fnames(k).name ' -ascii']); code I use to give the
>correct file (which is a string (converts number to string)), because
>the code below can't recognize the files correct.
>
> for k = 1 : length(fnames)
> fname = fnames(k).name;
> log = impordata('fname.txt'); // fname is variable so this gives a problem
> disp(log)
> end
>

No. You'd write

    impordata(fname)

not

    impordata(`fname.txt`)

i.e. use the actual variable name, like you did with the load. The
result of the dir() command you used.

any way, I am not really sure what is the issue any more.
You have few files, they have numbers, and you want to read the numbers
into a matrix or a vector, right?

If so, here is a good web page to review. This is really a very easy
and trivial problem if that is all there is to it.

http://www.mathworks.com/help/matlab/import_export/ways-to-import-text-files.html

good luck,
--Nasser

Subject: load column in from text file

From: Fiboehh

Date: 8 Dec, 2012 17:12:08

Message: 6 of 13

> d = dir('path\*.txt'); % the result of dir() is _not_ a fname but a
> directory structure; make the name more representative
>
> % preallocate your final result--I'm assuming B is no more than a vector
> here--if it's another array a cell array may be more suitable...
> BigAns=zeros(length(d),length(B));
>
> for k = 1:length(d)
> x=importdata(d(k).name); % pull the data into an array
> B = your_computation(x);
> BigAns(k) = B; % you can, of course, dispense w the intermediary
> end
> save results.txt BigAns -ASCII
>
> % the above assumes you're saving across the files instead of a single
> file for each of the input files. As you wrote it, you have overwritten
> the results and end up w/ only the last case after all that effort.
>
> --

I have just the same code as you, but MATLAB gives sometimes error with files which work fine if I load them in one by one :s
If my ascii-file is named 1_01.txt everything works fine, but if I use 2_01.txt it gives a fellowing error;

Error using importdata (line 137)
Unable to open file.

Error in ScienceWeek (line 11)
log = importdata(fnames(k).name);

Do you have a clue what is going wrong?

Subject: load column in from text file

From: dpb

Date: 8 Dec, 2012 17:35:22

Message: 7 of 13

On 12/8/2012 11:12 AM, Fiboehh wrote:
...

> I have just the same code as you, but MATLAB gives sometimes error with
> files which work fine if I load them in one by one :s
> If my ascii-file is named 1_01.txt everything works fine, but if I use
> 2_01.txt it gives a fellowing error;
>
> Error using importdata (line 137)
> Unable to open file.
>
> Error in ScienceWeek (line 11)
> log = importdata(fnames(k).name);
>
> Do you have a clue what is going wrong?

The error indicates the file probably doesn't exist -- perhaps you don't
have the correct working directory or it could possibly be protected or
the name isn't actually correct...

Other than that, from here it's hard to say.

Might try using debugger and when it errors poke around at the command
line and see what local context is at that point. Probably will be
illuminating...

--

Subject: load column in from text file

From: Fiboehh

Date: 9 Dec, 2012 01:10:08

Message: 8 of 13

dpb <none@non.net> wrote in message <k9vtoo$100$1@speranza.aioe.org>...
> On 12/8/2012 11:12 AM, Fiboehh wrote:
> ...
>
> > I have just the same code as you, but MATLAB gives sometimes error with
> > files which work fine if I load them in one by one :s
> > If my ascii-file is named 1_01.txt everything works fine, but if I use
> > 2_01.txt it gives a fellowing error;
> >
> > Error using importdata (line 137)
> > Unable to open file.
> >
> > Error in ScienceWeek (line 11)
> > log = importdata(fnames(k).name);
> >
> > Do you have a clue what is going wrong?
>
> The error indicates the file probably doesn't exist -- perhaps you don't
> have the correct working directory or it could possibly be protected or
> the name isn't actually correct...
>
> Other than that, from here it's hard to say.
>
> Might try using debugger and when it errors poke around at the command
> line and see what local context is at that point. Probably will be
> illuminating...
>
> --
Unbelieveble strange, but it just doesn't work with certain names. It must be something with the internal syntax in MATLAB. A bug or something, but if I import a text file in under the name 'a.txt' it gives an error: unable to open file, and if I rename the same file to '1_01.txt' it works :s :s
Totally nuts. I'm know manually converting all my files so that it can work automatically.
Sometimes a though how to avoid this bug/make it work without renaming manually the files?

Subject: load column in from text file

From: dpb

Date: 9 Dec, 2012 04:46:41

Message: 9 of 13

On 12/8/2012 7:10 PM, Fiboehh wrote:

...

> Unbelieveble strange, but it just doesn't work with certain names. It
> must be something with the internal syntax in MATLAB. A bug or
> something, but if I import a text file in under the name 'a.txt' it
> gives an error: unable to open file, and if I rename the same file to
> '1_01.txt' it works :s :s
> Totally nuts. I'm know manually converting all my files so that it can
> work automatically.
> Sometimes a though how to avoid this bug/make it work without renaming
> manually the files?

I've never used importdata() but it uses helper functions based on
extension name amongst perhaps other rules.

Unless you're naming something that isn't allowed by the OS there's
nothing in Matlab to prevent opening a file; I'm sure if you use debug
and work through what actually happens there will be a rational explanation.

Personally, I'd probably forget importdata() for a specific function for
the particular file I was opening...textread() (deprecated but still
useful because it returns array instead of cell), textscan(), fscanf(),
etc., and friends...

--

Subject: load column in from text file

From: dpb

Date: 9 Dec, 2012 14:02:41

Message: 10 of 13

On 12/8/2012 7:10 PM, Fiboehh wrote:
> dpb <none@non.net> wrote in message <k9vtoo$100$1@speranza.aioe.org>...
>> On 12/8/2012 11:12 AM, Fiboehh wrote:
>> ...
>>
>> > I have just the same code as you, but MATLAB gives sometimes error with
>> > files which work fine if I load them in one by one :s
>> > If my ascii-file is named 1_01.txt everything works fine, but if I use
>> > 2_01.txt it gives a fellowing error;
>> >
>> > Error using importdata (line 137)
>> > Unable to open file.
>> >
>> > Error in ScienceWeek (line 11)
>> > log = importdata(fnames(k).name);
>> >
>> > Do you have a clue what is going wrong?
>>
>> The error indicates the file probably doesn't exist -- perhaps you
>> don't have the correct working directory or it could possibly be
>> protected or the name isn't actually correct...
>>
>> Other than that, from here it's hard to say.
>>
>> Might try using debugger and when it errors poke around at the command
>> line and see what local context is at that point. Probably will be
>> illuminating...
>>
>> --
> Unbelieveble strange, but it just doesn't work with certain names. It
> must be something with the internal syntax in MATLAB. A bug or
> something, but if I import a text file in under the name 'a.txt' it
> gives an error: unable to open file, and if I rename the same file to
> '1_01.txt' it works :s :s
> Totally nuts. I'm know manually converting all my files so that it can
> work automatically.
> Sometimes a though how to avoid this bug/make it work without renaming
> manually the files?

I just looked at the importdata() function here -- here's the beginning
of the code wherein the error message you have comes from...

...
if strcmpi(FileName,'-pastespecial')
     % fetch data from clipboard
    ...[snip]...
    FileType = '';
    fromClipboard = logical(1);
else
     % fetch data from file
     if exist(FileName) ~= 2
         error('Unable to open file.')
     end
     ....
...


As noted earlier, the problem is that either your filename is wrong, the
working directory isn't where the file is located or it's protected or
something of the sort is going on. I'm sure there's a reason other than
that it is the name itself (again unless you've gotten some illegal
hidden/non-printing character embedded in the name or you've somehow
otherwise used an OS-prohibited name.

Use the debugger and set a break at the line where the above test (or
its equivalent in your ML release) and see what actually is the situation...

--

Subject: load column in from text file

From: Steven_Lord

Date: 10 Dec, 2012 03:31:50

Message: 11 of 13



"Fiboehh " <stig_dooms@hotmail.com> wrote in message
news:ka0odg$clv$1@newscl01ah.mathworks.com...
> dpb <none@non.net> wrote in message <k9vtoo$100$1@speranza.aioe.org>...
>> On 12/8/2012 11:12 AM, Fiboehh wrote:
>> ...
>>
>> > I have just the same code as you, but MATLAB gives sometimes error with
>> > files which work fine if I load them in one by one :s
>> > If my ascii-file is named 1_01.txt everything works fine, but if I use
>> > 2_01.txt it gives a fellowing error;
>> >
>> > Error using importdata (line 137)
>> > Unable to open file.
>> >
>> > Error in ScienceWeek (line 11)
>> > log = importdata(fnames(k).name);
>> >
>> > Do you have a clue what is going wrong?
>>
>> The error indicates the file probably doesn't exist -- perhaps you don't
>> have the correct working directory or it could possibly be protected or
>> the name isn't actually correct...
>>
>> Other than that, from here it's hard to say.
>>
>> Might try using debugger and when it errors poke around at the command
>> line and see what local context is at that point. Probably will be
>> illuminating...
>>
>> --
> Unbelieveble strange, but it just doesn't work with certain names. It must
> be something with the internal syntax in MATLAB. A bug or something, but
> if I import a text file in under the name 'a.txt' it gives an error:
> unable to open file, and if I rename the same file to '1_01.txt' it works
> :s :s

It _could_ be a bug, but I doubt it.

Try using FOPEN with two outputs to open the file. If the first output
is -1, look at the second output. It will be the message from the OS
hopefully identifying why it couldn't open the file. That may shed some
light on why you received an error from IMPORTDATA. My guess is either that
you have too many files open simultaneously or you already have it open in
some other program.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: load column in from text file

From: Fiboehh

Date: 11 Dec, 2012 20:30:08

Message: 12 of 13

>
> It _could_ be a bug, but I doubt it.
>
> Try using FOPEN with two outputs to open the file. If the first output
> is -1, look at the second output. It will be the message from the OS
> hopefully identifying why it couldn't open the file. That may shed some
> light on why you received an error from IMPORTDATA. My guess is either that
> you have too many files open simultaneously or you already have it open in
> some other program.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thanx all for the support, I still don't know exactly why it doesn't work when I use a directory that is not the working directory (where my program is stored). But it turns out it only works when the directory where the text files are saved is the same as matlab script. Other directory doesn't work always, strange enough it does with certain names.
So I can go further, and just have a lot of files in my working directory, but yeah it works :)
Once again thanx to help me.

Subject: load column in from text file

From: dpb

Date: 11 Dec, 2012 20:47:24

Message: 13 of 13

On 12/11/2012 2:30 PM, Fiboehh wrote:
...

> Thanx all for the support, I still don't know exactly why it doesn't
> work when I use a directory that is not the working directory (where my
> program is stored). But it turns out it only works when the directory
> where the text files are saved is the same as matlab script. Other
> directory doesn't work always, strange enough it does with certain names.
> So I can go further, and just have a lot of files in my working
> directory, but yeah it works :)
> Once again thanx to help me.

That then is almost certainly a problem in building the file name, a
protection problem or as Steven says, perhaps you've built the file
somewhere else and saved it but it's still open in the other app or
something like that.

Need actual code and error messages and data but whatever it is, it's
not magic (and as Steven also says, unlikely to be a Matlab bug).

--

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