Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Read Part of String

Subject: Read Part of String

From: M@

Date: 17 Jul, 2008 14:04:26

Message: 1 of 13

Hello,

This may sound like a rediculous question, but a solution would make
my day a hell of a lot easier.

I have two files, for example we'll call them "F423.dat" and
"F423_spectra.dat" i have written a program that will import all
files in the directory (directory is a mix of .dat files and
_spectra.dat files) but i'd like the program to perform different
operations on the different types of files. I was wondering if MATLAB
could read part of a string (namely distinguish between the normal
files and the _spectra files) and sort the files accordingly?

Thanks,
mb

Subject: Read Part of String

From: Carlos Adrian Vargas Aguilera

Date: 17 Jul, 2008 14:14:02

Message: 2 of 13


Try

>> infiles = ls(['F423' * '.dat']);
>> file01 = deblank(infiles(1,:));
>> file02 = deblank(infiles(2,:));

Carlos

Subject: Read Part of String

From: David

Date: 17 Jul, 2008 14:16:02

Message: 3 of 13

"M@" <matthew.betti@gmail.com> wrote in message <ae8de555-
911d-4566-9e9f-165c2d732dba@d1g2000hsg.googlegroups.com>...
> Hello,
>
> This may sound like a rediculous question, but a
solution would make
> my day a hell of a lot easier.
>
> I have two files, for example we'll call them "F423.dat"
and
> "F423_spectra.dat" i have written a program that will
import all
> files in the directory (directory is a mix of .dat files
and
> _spectra.dat files) but i'd like the program to perform
different
> operations on the different types of files. I was
wondering if MATLAB
> could read part of a string (namely distinguish between
the normal
> files and the _spectra files) and sort the files
accordingly?
>
> Thanks,
> mb

yes, you can access strings a character at a time just
like a vector of chars. Or you could use the many string
parsing and searching functions.

Subject: Read Part of String

From: dpb

Date: 17 Jul, 2008 15:00:32

Message: 4 of 13

M@ wrote:
> Hello,
>
> This may sound like a rediculous question, but a solution would make
> my day a hell of a lot easier.
>
> I have two files, for example we'll call them "F423.dat" and
> "F423_spectra.dat" i have written a program that will import all
> files in the directory (directory is a mix of .dat files and
> _spectra.dat files) but i'd like the program to perform different
> operations on the different types of files. I was wondering if MATLAB
> could read part of a string (namely distinguish between the normal
> files and the _spectra files) and sort the files accordingly?

help strings

One alternative that comes to mind would be to do the search on
"*spectra.dat" files and use the returned list instead of all files.

Another depending on your OS would be something like a file
specification of "f???.dat" to only return the files of form fnnn.dat

Many, many choices... :)

--

Subject: Read Part of String

From: Rune Allnor

Date: 17 Jul, 2008 15:16:28

Message: 5 of 13

On 17 Jul, 16:04, "M@" <matthew.be...@gmail.com> wrote:
> Hello,
>
> This may sound like a rediculous question, but a solution would make
> my day a hell of a lot easier.
>
> I have two files, for example we'll call them "F423.dat" and
> "F423_spectra.dat" =A0i have written a program that will import all
> files in the directory (directory is a mix of .dat files and
> _spectra.dat files) but i'd like the program to perform different
> operations on the different types of files. =A0I was wondering if MATLAB
> could read part of a string (namely distinguish between the normal
> files and the _spectra files) and sort the files accordingly?

Regular expressions?

Rune

Subject: Read Part of String

From: M@

Date: 17 Jul, 2008 16:54:36

Message: 6 of 13

Thank you all very much for the timely and very useful advice!

I'm still having a problem though with the sorting. Thanks to all the
input above, I can get a cell array of just the _spectra files.
Although, i need a separate array of all files without the
_spectra.dat files name. Namely, an array of F423.dat, F424.dat,
F425.dat etc. dpb, I've tried using 'f???.dat' to no avail.

Subject: Read Part of String

From: dpb

Date: 17 Jul, 2008 17:40:28

Message: 7 of 13

M@ wrote:
> Thank you all very much for the timely and very useful advice!
>
> I'm still having a problem though with the sorting. Thanks to all the
> input above, I can get a cell array of just the _spectra files.
> Although, i need a separate array of all files without the
> _spectra.dat files name. Namely, an array of F423.dat, F424.dat,
> F425.dat etc. dpb, I've tried using 'f???.dat' to no avail.

Which OS? The "?" is a wildcard for a single-character match on Windows
platforms; I don't know enough about unix-like shells to know what is
appropriate there but I would presume there's some incantation to do the
job.

I can't help w/ the regexp solution as my version of Matlab predates its
introduction, sorry.

There would certainly be ways via the string functions in Matlab to
process the full directory away and find those which contain "spectra"
(findstr() comes to mind) and the remaining would be those w/o...

--

Subject: Read Part of String

From: M@

Date: 17 Jul, 2008 18:08:12

Message: 8 of 13

On Jul 17, 1:40=A0pm, dpb <n...@non.net> wrote:
> M@ wrote:
> > Thank you all very much for the timely and very useful advice!
>
> > I'm still having a problem though with the sorting. =A0Thanks to all th=
e
> > input above, I can get a cell array of just the _spectra files.
> > Although, i need a separate array of all files without the
> > _spectra.dat files name. =A0Namely, an array of F423.dat, F424.dat,
> > F425.dat etc. =A0dpb, I've tried using 'f???.dat' to no avail.
>
> Which OS? =A0The "?" is a wildcard for a single-character match on Window=
s
> platforms; I don't know enough about unix-like shells to know what is
> appropriate there but I would presume there's some incantation to do the
> job.
>
> I can't help w/ the regexp solution as my version of Matlab predates its
> introduction, sorry.
>
> There would certainly be ways via the string functions in Matlab to
> process the full directory away and find those which contain "spectra"
> (findstr() comes to mind) and the remaining would be those w/o...
>
> --

I am running windows, and that "?" wildcard would certainly be what I
would like to use, just to try and keep things simple. Although it
does not seem to work. Is there something special about MATLAB R14
Student Version that would not allow "?" to work as a wildcard?

Subject: Read Part of String

From: dpb

Date: 17 Jul, 2008 18:25:56

Message: 9 of 13

M@ wrote:
...
> I am running windows, and that "?" wildcard would certainly be what I
> would like to use, just to try and keep things simple. Although it
> does not seem to work. Is there something special about MATLAB R14
> Student Version that would not allow "?" to work as a wildcard?

No, it's a bug and has been around since at least Ver 5.3 (oldest I have
loaded). The Matlab dir() function isn't treating the "?" as a wildcard
character on Windows and passing it to the OS.

However, you can work around it in the following way...

 >> dir('c:\temp\f*.dat')

f123.dat f123_spec.dat f124.dat f124_spec.dat

(Demonstrates what's in c:\temp of interest)

 >> [stat,d]=dos('dir /b c:\temp\f???.dat')
stat =
      0
d =
f123.dat
f124.dat

--

Subject: Read Part of String

From: M@

Date: 17 Jul, 2008 19:59:08

Message: 10 of 13

On Jul 17, 2:25=A0pm, dpb <n...@non.net> wrote:
> M@ wrote:
>
> ...
>
> > I am running windows, and that "?" wildcard would certainly be what I
> > would like to use, just to try and keep things simple. =A0Although it
> > does not seem to work. =A0Is there something special about MATLAB R14
> > Student Version that would not allow "?" to work as a wildcard?
>
> No, it's a bug and has been around since at least Ver 5.3 (oldest I have
> loaded). =A0The Matlab dir() function isn't treating the "?" as a wildcar=
d
> character on Windows and passing it to the OS.
>
> However, you can work around it in the following way...
>
> =A0>> dir('c:\temp\f*.dat')
>
> f123.dat =A0 =A0 =A0 f123_spec.dat =A0f124.dat =A0 =A0 =A0 f124_spec.dat
>
> (Demonstrates what's in c:\temp of interest)
>
> =A0>> [stat,d]=3Ddos('dir /b c:\temp\f???.dat')
> stat =3D
> =A0 =A0 =A0 0
> d =3D
> f123.dat
> f124.dat
>
> --

this code turns the file names to character, as opposed to strings.
i.e. d(1)=3Df as opposed to the more useful d(1)=3D 'f123.dat'

I'm sure there are many ways of getting characters back to strings,
and i'm sorry for the hand-holding approach to coding i'm taking. I'm
just the closest thing this lab has to a programmer, and thus the
burden was laid. =3DP

Subject: Read Part of String

From: dpb

Date: 17 Jul, 2008 22:31:51

Message: 11 of 13

M@ wrote:
> On Jul 17, 2:25 pm, dpb <n...@non.net> wrote:
...
>> [stat,d]=dos('dir /b c:\temp\f???.dat')
>> stat =
>> 0
>> d =
>> f123.dat
>> f124.dat
...
> this code turns the file names to character, as opposed to strings.
> i.e. d(1)=f as opposed to the more useful d(1)= 'f123.dat'
>
> I'm sure there are many ways of getting characters back to strings,
> and i'm sorry for the hand-holding approach to coding i'm taking. I'm
> just the closest thing this lab has to a programmer, and thus the
> burden was laid. =P

I admit I didn't do anything but make sure the dos() function did end up
w/ the OS "doing the right thing" so only saw the displayed output was
the correct file names, not how they were returned.

Turns out the names are terminated by lf character (ASCII decimal 10) so
strtok() will work to return each filename from the array.

Try something like

r=d;while length(r)>1, [f,r]=strtok(r), end

Normally would like the while condition to be something like ~isempty(r)
but in this case there's a lf after the last filename also and the
remainder of the string after the token is returned also contains the
terminating token.

Alternatively, know that, one could also be clever as the following shows...

 >> r=d;while ~isempty(r), [f,r]=strtok(r); r=r(2:end); disp(f), end
f123.dat
f124.dat

 >>

Subject: Read Part of String

From: dpb

Date: 17 Jul, 2008 22:38:15

Message: 12 of 13

dpb wrote:
...
> No, it's a bug and has been around since at least Ver 5.3 (oldest I have
> loaded). The Matlab dir() function isn't treating the "?" as a wildcard
> character on Windows and passing it to the OS.
...

Well, that's not really stated correctly -- it appears of course that
the "?" does get passed to the OS, but somehow the API used doesn't
honor it. Interestingly, to see if there was something funky in the
Win32 API, I tried the VB Dir$ function and it does honor it. So,
either VB does a fixup or there's something not kosher about the Matlab
call...wherever it actually is, it's a bug as the routine doesn't behave
properly for the underlying OS...

I don't think it had ever been anything I had noticed in Matlab prior to
now, though... :)

--

Subject: Read Part of String

From: dpb

Date: 18 Jul, 2008 18:16:04

Message: 13 of 13

dpb wrote:
> dpb wrote:
> ...
>> No, it's a bug and has been around since at least Ver 5.3 (oldest I
>> have loaded). The Matlab dir() function isn't treating the "?" as a
>> wildcard character on Windows and passing it to the OS.
...

> I don't think it had ever been anything I had noticed in Matlab prior to
> now, though... :)

It doesn't show as a currently listed but so I reported the anomalous
behavior as a bug...

--

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics