Extract filename

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Extract filename

theozh
With the following commands (under Windows) I would like to plot several
datafiles located in several subdirectories, which obey a certain name
scheme.

FileList = system("dir /B /S Data*.dat")
plot for [FileName in FileList] FileName u 1:2 w l title FileName

However, with this, FileName always contains the full path,
e.g. C:\SubDir1\SubSubDir2\Test3\Blabla4\Data007.dat
which is not very helpful in the legend.

Any ideas how I can separate the path from the filename in gnuplot,
or even just extract what "*" was, e.g. the number "007"?
substr() will not work because subpaths can have different length.
Is there anything like regular expressions in gnuplot?
Thank you for any hints.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
gnuplot-info mailing list
[hidden email]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extract filename

BBands
There are some example of gnuplot's strong variable capabilities here that
should solve your needs.

http://gnuplot.sourceforge.net/demo/stringvar.html

      John

On Wed, May 24, 2017 at 12:36 PM, theozh <[hidden email]> wrote:

> With the following commands (under Windows) I would like to plot several
> datafiles located in several subdirectories, which obey a certain name
> scheme.
>
> FileList = system("dir /B /S Data*.dat")
> plot for [FileName in FileList] FileName u 1:2 w l title FileName
>
> However, with this, FileName always contains the full path,
> e.g. C:\SubDir1\SubSubDir2\Test3\Blabla4\Data007.dat
> which is not very helpful in the legend.
>
> Any ideas how I can separate the path from the filename in gnuplot,
> or even just extract what "*" was, e.g. the number "007"?
> substr() will not work because subpaths can have different length.
> Is there anything like regular expressions in gnuplot?
> Thank you for any hints.
>
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> gnuplot-info mailing list
> [hidden email]
> Membership management via: https://lists.sourceforge.net/
> lists/listinfo/gnuplot-info
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
gnuplot-info mailing list
[hidden email]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extract filename

Ethan A Merritt-2
In reply to this post by theozh
Assuming that the subdirectories are not themselves named Datasomething,
I'd use something like

    chop(fullname) = fullname[ strstrt(fullname,"Data") : * ]

    plot for [FileName in FileList] FileName u 1:2 w l title chop(FileName)


On Wed, May 24, 2017 at 12:36 PM, theozh <[hidden email]> wrote:

> With the following commands (under Windows) I would like to plot several
> datafiles located in several subdirectories, which obey a certain name
> scheme.
>
> FileList = system("dir /B /S Data*.dat")
> plot for [FileName in FileList] FileName u 1:2 w l title FileName
>
> However, with this, FileName always contains the full path,
> e.g. C:\SubDir1\SubSubDir2\Test3\Blabla4\Data007.dat
> which is not very helpful in the legend.
>
> Any ideas how I can separate the path from the filename in gnuplot,
> or even just extract what "*" was, e.g. the number "007"?
> substr() will not work because subpaths can have different length.
> Is there anything like regular expressions in gnuplot?
> Thank you for any hints.
>
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> gnuplot-info mailing list
> [hidden email]
> Membership management via: https://lists.sourceforge.net/
> lists/listinfo/gnuplot-info
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
gnuplot-info mailing list
[hidden email]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extract filename

theozh
Thanks, John & Ethan for your immediate response.

> There are some example of gnuplot's strong variable capabilities here that
> should solve your needs.
> http://gnuplot.sourceforge.net/demo/stringvar.html
>

@John, thanks for the link, but strong string variable capabilities? Are
you serious?
It would be nice to find the last key (e.g. '\') in a word. Or at least
a function to reverse a string. Best would be of course regular
expressions ;-).


> Assuming that the subdirectories are not themselves named Datasomething,
Well, I cannot be sure that this will always be the case.

> I'd use something like
>
>     chop(fullname) = fullname[ strstrt(fullname,"Data") : * ]
>
>     plot for [FileName in FileList] FileName u 1:2 w l title chop(FileName)
>
@Ethan, yes, this works with the above mentioned limitations.

Based on your suggestion, the following is a cumbersome workaround for a
function to extract the file name (at least to 6 levels of
subdirectories, but can be extended).


TestFile = 'C:\sub1\sub2\sub3\sub4\Data007.dat'

chop1(FullName) = FullName[strstrt(FullName,'\')+1:*]
chop2(FullName) = chop1(FullName)[strstrt(chop1(FullName),'\')+1:*]
chop3(FullName) = chop2(FullName)[strstrt(chop2(FullName),'\')+1:*]
chop4(FullName) = chop3(FullName)[strstrt(chop3(FullName),'\')+1:*]
chop5(FullName) = chop4(FullName)[strstrt(chop4(FullName),'\')+1:*]
chop6(FullName) = chop5(FullName)[strstrt(chop5(FullName),'\')+1:*]
chop(FullName) = chop6(FullName)[strstrt(chop6(FullName),'\')+1:*]

FileName = chop(TestFile)
print FileName

If anybody knows a more elegant or smarter solution, please let me know.



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
gnuplot-info mailing list
[hidden email]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extract filename

thse
> If anybody knows a more elegant or smarter solution, please let me know.

Call the function recursively:

TestFile = 'C:\sub1\sub2\sub3\sub4\Data007.dat'
chop(fn) = strstrt(fn,'\')?chop(fn[strstrt(fn,'\')+1:*]):fn
print chop(TestFile)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extract filename

BBands
In reply to this post by theozh
Er, that was a typo. I meant to write 'string variables'.

    John

On Thu, May 25, 2017 at 6:17 AM, theozh <[hidden email]> wrote:

> Thanks, John & Ethan for your immediate response.
>
> > There are some example of gnuplot's strong variable capabilities here
> that
> > should solve your needs.
> > http://gnuplot.sourceforge.net/demo/stringvar.html
> >
>
> @John, thanks for the link, but strong string variable capabilities? Are
> you serious?
> It would be nice to find the last key (e.g. '\') in a word. Or at least
> a function to reverse a string. Best would be of course regular
> expressions ;-).
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
gnuplot-info mailing list
[hidden email]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extract filename

theozh
In reply to this post by thse
Thanks a lot, Thomas, that's probably the shortest and most elegant way
to do it.
I was not aware that recursive functions are possible.

The following code will give only the content of the wildcard in the
title/legend/key.

### start code
BeforeWC = "Data"
AfterWC = ".dat"
FileList = system("dir /B /S ".BeforeWC."*".AfterWC)
chop(fn) = strstrt(fn,'\')?chop(fn[strstrt(fn,'\')+1:*]):fn

Wildcard(fn) = chop(fn)[strlen(BeforeWC)+1:strlen(chop(fn))-strlen(AfterWC)]

plot for [FileName in FileList] FileName u 1:2 w l title
Wildcard(FileName) noenhanced
### end code

If anybody has again a smarter solution please let me know.

Are there any chances (now or in the future) to somehow use "Regular
expressions" in gnuplot?
Thanks, Theo.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
gnuplot-info mailing list
[hidden email]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extract filename

Ethan A Merritt-2
On Thu, May 25, 2017 at 11:28 AM, theozh <[hidden email]> wrote:

>
> Are there any chances (now or in the future) to somehow use "Regular
> expressions" in gnuplot?
> Thanks, Theo.
>
> Can you fill in that idea a bit?   Do you envision this as a binary test
'does string A match regexp B? yes/no'?   Something more complicated?
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
gnuplot-info mailing list
[hidden email]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extract filename

theozh
>
>> Can you fill in that idea a bit?   Do you envision this as a binary test
> 'does string A match regexp B? yes/no'?   Something more complicated?
>
Honestly, it's not thought through thoroughly. Just when trying to
extract the filename from the full filename I thought it would be nice
if in above example an expression something like |.*\\Data(.*)\.dat|
directly returned the "007". Well, now I have a rather compact solution.

On the otherhand, when creating a list of data files to be plotted it
would be nice if the search pattern in
  FileList = system(dir /B /S Data*.dat)
could be a bit more sophisticated than a simple wildcard. I know that
this is a Windows system command and gnuplot doesn't have any influence
on it. Although, gnuplot could filter it further and/or "distribute" the
files to sublists which are then plotted in a single plot or multiplot
environment. I need to find a practical example to illustrate. But
probably one can also find a solution with the already existing gnuplot
commands.
I guess a binary regexp test together with the ternary operator could be
quite powerful.
Well, as I said, it's not fully thought out...


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
gnuplot-info mailing list
[hidden email]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
Loading...