Message Boards Message Boards

0
|
7307 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Setting left hand side variable names while importing multiple files

Posted 9 years ago

Hi,

I've a set of files obtained from an instrument with filenames in the format "insXpY.mat", X and Y being integers. For example, "ins8p1.mat", "ins8p7.mat",... "ins8p41.mat" and so on...

I can import any such file using

d8p41=Import["ins8p41.mat"]

and then I can process the data array 'd8p41' in whatever way I like. This however gets quite exhausting when I have multiple folders with several such files in each folder. What I would like to do (for every folder) is have a loop in which, say the j^th file (with j being uniquely related to X & Y; see below for that) is imported with the variable name set as dXpY. The unique mapping between j and X & Y can be simply made because

fn = FileNames["*mat"]

returns me a list of the files with extension 'mat'. However, I have not been able to figure out how to set the variable name on the left hand side during the file importing, using the same kind of mapping. Any inputs in this regard would be very welcome!

POSTED BY: Jay Ann
2 Replies
Posted 9 years ago
 Map[Import[#, "Table"] &,
  Table["file_path\\ins8p." <>
    StringTake["000" <> ToString[i], -4], {i, 1, 10}]]

You may try this, not sure it will work for you..

POSTED BY: Okkes Dulgerci

You can do this the way you describe. In most programming languages, you wouldn't be able to what you are describing. There are very good reasons for this.

Instead of programmatically creating variables, create a list:

files = Map[#->Import[#]&, FileNames["*mat"]]

This gives you a list of rules. The left side of the rule is the file name. The right side of the rule is the data. You can lookup information in it:

Lookup[files, "ins8p41.mat"]
POSTED BY: Sean Clarke
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