3
$\begingroup$

I have some scv files, they are named based on the testing condition from the simulation runs, basically from blk50_25 to blk300_150, the first number after "blk" was ranged from 50 to 300 incremented by 10, and 2nd number after "_" was ranged from 25 to 150 incremented by 5. (i.e first file blk50_25, 2nd file blk60_30, 3rd file blk70_35, etc.) I'm trying to write a loop to call all the files and do an x-y plot of each, here's what I have done

data = Import["/Users/Desktop/ConvTestOP/blk50_25.csv", "CSV"]
MD = Table[{data[[j, 1]], data[[j, 2]]}, {j, 1, Length[data]}];
Reverse /@ MD
ListPlot[MD[[All, {2, 1}]]]

I'm not sure how to put variables in the filename, much appreciated if anyone can give me a hint.

$\endgroup$
6
$\begingroup$

For old versions:

Table[ToString[StringForm["/Users/Desktop/ConvTestOP/blk`1`_`2`.csv",
                          10 k + 50, 5 k + 25]], {k, 0, 25}]

For new versions:

Table[StringTemplate["/Users/Desktop/ConvTestOP/blk`i`_`j`.csv"] @
      <|"i" -> 10 k + 50, "j" -> 5 k + 25|>, {k, 0, 25}]

Both generate a list of the filenames following your system; modify the Table[] as needed (e.g. incorporating Import[] and ListPlot[]).

$\endgroup$
  • $\begingroup$ Thanks for the speed response, when I name the data, is there a way to do something like data_i_j = Import["/Users/Desktop/ConvTestOP/blki_j.csv", "CSV"] where i and j are based on your code above? $\endgroup$ – Gvxfjørt Oct 4 '16 at 3:14
  • $\begingroup$ @Gavy, you can do something like Table[data[10 k + 50, 5 k + 25] = (* stuff *)] for that, and then refer to data[50, 25], data[60, 30], etc. $\endgroup$ – J. M. will be back soon Oct 4 '16 at 3:26
4
$\begingroup$

Alternatively, you can grab all the file names of the appropriate type and read them in. This means you don't need to know the exact names of the files in advance.

allfiles = FileNames["*", "/Users/Desktop/ConvTestOP/"];
allData = Import[#] & /@ allfiles;

then build your plots directly from allData.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.