Message Boards Message Boards

0
|
3177 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

How do I use a string list element as a variable?

Posted 10 years ago

Hi all,

After I list the file names of a directory (Filenames[ ]) I would like tu use the file names as a variable (without the extension) . How do I do this?

For example:


In[18]:= SetDirectory["C:\\Users\\AG\\Google Drive\\Diploma cuccok\\remesh34\\D0_res"]
Out[18]= C:\Users\AG\Google Drive\Diploma cuccok\remesh34\D0_res
In[19]:= Fnames=TableForm[FileNames[]]
Out[19]//TableForm= m_inlet_rm34n_d0_stac_s0.out
m_intwin_rm34n_d0_stac_s0.out
m_outlet_rm34n_d0_stac_s0.out
point130_rm34n_d0_stac_s0.out
point131_rm34n_d0_stac_s0.out
point132_rm34n_d0_stac_s0.out


In[20]:= StringJoin[StringSplit[FileBaseName[Fnames[[1,1]]],"_"]]
Out[20]= minletrm34nd0stacs0
In[21]:= StringJoin[StringSplit[FileBaseName[Fnames[[1,1]]],"_"]]=Import[Fnames[[1, 1]], "Table"];
dl1 = Length[Data01]
During evaluation of In[21]:= Set::write: Tag StringJoin in StringJoin[{m,inlet,rm34n,d0,stac,s0}] is Protected. >>
Out[22]= 0

I would like to use "minletrm34nd0stacs0" as a variable name. This file "mintwinrm34nd0stac_s0.out" contains strings and numbers also. Only the first 3 row contains the string characters, but I do not need them, just the number rows.

In a bigger view, I would like to automatize the reading of the result files.

POSTED BY: gabringa

Hi,

Assuming single occurrence of "." in a file name you can try something like this:

Clear["Global`*"]
SetDirectory[NotebookDirectory[]] ;
(* Generate test files *)
MapThread[Export[#1<>".dat",#2,"Table"] &,{CharacterRange["a","z"],Table[{"ch1","ch2","ch3",i,i^2,i^3},{i,1,26}]}] ;
(* Set *)
Set[
    Evaluate[ToExpression[StringTake[#,Sequence@@StringPosition[#,"."]-1]& /@ #]],
    Evaluate[Import[#,"Table"][[4;;-1]] & /@ #]
] & @ FileNames["*.dat"] ;
(* Test *)
{a,b,c}

I.M.

POSTED BY: Ivan Morozov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract