Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Math Forum / Math Software / MATLAB / July 2008



Tip: Looking for answers? Try searching our database.

Read Part of String

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
M@ - 17 Jul 2008 15:04 GMT
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
Carlos Adrian Vargas Aguilera - 17 Jul 2008 15:14 GMT
Try

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

Carlos
David - 17 Jul 2008 15:16 GMT
"M@" <matthew.betti@gmail.com> wrote in message <ae8de555-
911d-4566-9e9f-165c2d732dba@d1g2000hsg.googlegroups.com>...
> Hello,
>
[quoted text clipped - 11 lines]
> 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.
dpb - 17 Jul 2008 16:00 GMT
> Hello,
>
[quoted text clipped - 8 lines]
> 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... :)

--
Rune Allnor - 17 Jul 2008 16:16 GMT
> Hello,
>
[quoted text clipped - 8 lines]
> could read part of a string (namely distinguish between the normal
> files and the _spectra files) and sort the files accordingly?

Regular expressions?

Rune
M@ - 17 Jul 2008 17:54 GMT
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.
dpb - 17 Jul 2008 18:40 GMT
> Thank you all very much for the timely and very useful advice!
>
[quoted text clipped - 3 lines]
> _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...

--
M@ - 17 Jul 2008 19:08 GMT
> > Thank you all very much for the timely and very useful advice!
>
[quoted text clipped - 17 lines]
>
> --

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?
dpb - 17 Jul 2008 19:25 GMT
...
> 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

--
M@ - 17 Jul 2008 20:59 GMT
> ...
>
[quoted text clipped - 23 lines]
>
> --

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
dpb - 17 Jul 2008 23:31 GMT
...
>> [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'
[quoted text clipped - 3 lines]
> 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
dpb - 17 Jul 2008 23:38 GMT
...
> 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... :)

--
dpb - 18 Jul 2008 19:16 GMT
> ...
>> 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...

--
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.