1
$\begingroup$

I made a function called "SolveLinearSystem[]" that solves,as its name suggests, a linear system of any amount of variables. It takes the path of a text file as its only argument. The function then proceeds to import and calculate the solution.

I currently have a bunch of files stored as text files individually, P1.txt, P2.txt,...,P15.txt which contain a linear system in them.

Now I need to find a way to query a directory so it shows me all text files in there, and then apply my "SolveLinearSystem[]" to each file.

My ultimate goal would be to create an unknown function that accepts a directory (Unknown[dir_]) as argument and then outputs the solution to each system similar to this:

P1.txt
{-0.385248, 0.236472, 1.03691, 1.10608, 1.14523, -1.51569, 1.14748, \
-0.961359, -1.53681, -0.8193, -0.949131}
P2.txt
{22.3934, -6.33219, -9.6342, 16.3643, -9.68272, -18.3644, -5.3851, \
3.75394, -25.4234, -7.91972, -0.370032}
P3.txt
{0.629152, -1.03705, -1.15584, 1.5237, -0.760991, 1.25683, 2.26095, \
-1.6362, 0.728382, -0.160247, -0.467798, -0.100069, -0.743948, \
0.720801, 0.904125}
...etc.

Which represent the set of solutions to each system. Thanks in advance for your help, guys!

$\endgroup$
2
  • $\begingroup$ FileNames["P*.txt"]? Then Import[], use your function, and then use Put[]. $\endgroup$ Commented Mar 17, 2016 at 2:48
  • $\begingroup$ Dang havent looked at this in years, but ended up finding a solution after all. Cant remember what it was LOL graduated years ago. Thanks to everyone who posted on this thread. $\endgroup$
    – TheYogi
    Commented Aug 18, 2021 at 19:22

1 Answer 1

1
$\begingroup$

You can change current working directory with SetDirectory, read docs for more info.

From the docs: You can see a list of all files in the current working directory by evaluating FileNames[].

You could Map (docs) SolveLinearSystem[] to the output of FileNames[], once you have set the working directory to the files' location.

<3 <3 Documentation!

$\endgroup$
2
  • $\begingroup$ I'd imagine, one ought not to Apply, but rather Map SolveLinearSystem to the output of Filenames[] $\endgroup$
    – LLlAMnYP
    Commented Mar 17, 2016 at 15:29
  • $\begingroup$ I think you're right- edited :) $\endgroup$
    – laudiacay
    Commented Mar 18, 2016 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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