Tag Info

New answers tagged

1

Low-level file operators like Write won't work here because OutputStreams (such as you get with OpenWrite and OpenAppend) can't have their StreamPosition set before the end of the file. In general overwriting characters in an existing file isn't terribly trivial; you can use c-functions likefile_ptr = fopen(file, "rb+") to do this, but you're overwriting ...


3

In this case, it is advisable to use the Export command: Export["test", data, "Table"] Leads to: Alternative one can use TableSpacing i.e. manipulate space between rows or columns, OutputForm[TableForm[data, TableSpacing -> {0, 0}]] >> "test" this leads to; Edit One can control Accuracy by: data1 = SetAccuracy[RandomReal[{-1, 1}, {3, ...


3

One workaround I found is this: getRand[] := AbortProtect@Module[{stream, res}, stream = OpenRead["!head -c 4 /dev/random", BinaryFormat -> True]; res = BinaryRead[stream, "UnsignedInteger32"]; Close[stream]; res ] Tested on OS X and Linux.


5

A simple way to read the last line of a file uses ReadList to get all lines from the file and then returns the last element of the list: lastLine[file_] := ReadList[file, String] // Last This code ignores the possibility of an empty file. If we care about such an eventuality, we can deal with it using a bit more logic: lastLine[file_] := ReadList[file, ...


6

In Mathematica 10.0.0 for Windows, I have experienced similar problems. When non-ASCII characters were placed after \ in a string, they were decoded in a strange way. (Character '\' is used as a path separator in Windows). ToCharacterCode["\\a", "Unicode"](*OK*) {92, 97} ToCharacterCode["\\", "Unicode"](*OK*) {92} ToCharacterCode["μ", "Unicode"](*OK*) ...


3

I would use StringFreeQ: (* files = {"10.txt", "11.txt", . . ., "inelasticov3-8.txt", "inelasticov3-9.txt"}; *) Select[files, StringFreeQ[#, "inelastic"] &] {"10.txt", "11.txt", "12.txt", "13.txt", "14.txt", "15.txt", "16.txt", "17.txt", "18.txt", "19.txt", "1.txt", "20.txt", "2.txt", "3.txt", "4.txt", "5.txt", "6.txt", "7.txt", "8.txt", ...


1

Several issues: You don't have to OpenWrite a file when you use Export. Export will do everything for you. OpenWrite is for situations where you want to do low-level file operations. You can use Element[{a,b,c},..] to say say that a,b,c should be Integers. When you want to store values of EvaluationMonitor it is maybe easier to use Sow and Reap. {result, ...


2

To get the file list without the files containing "inelastic", you can use: Cases[{your file list here}, x_String /; StringMatchQ[x, "*inelastic*"] == False] Then, to import them: Import[#] & /@ % One-step solution: Import[#] & /@ Cases[{your file list here}, x_String /; StringMatchQ[x, "*inelastic*"] == False] Edit: Out of curiosity, ...


2

You need to use the pattern "*inelastic*.txt" in FileNames. This will only return the list of files you need. Then use Map with pure functions (Function) to import all of them in one go: Import[#, "Table"]& /@ fileNames or similar. To use everything except file names with "inelastic", you can use Select,Cases,DeleteCases, etc. withStringMatchQ`. ...


1

Not really a Mathematica specific question but in Mac OS X absolute paths begin with a / character exactly as you give in your desired result, so SetDirectory["/Library/Mathematica"]


1

If you want Mathematica to interpret it as an absolute path, why don't you pass it one? In Windows: SetDirectory["D:\\Library\\Mathematica"] will set the path to D:\Library\Mathematica.


2

This somewhat depends on your specific file names, desired target filename and location, the details of procedure etc. There would be a number of approaches. I post this for motivation. In the following I assume: all the files for processing are in the same directory (and number 0 Mod 5) they have a sequential nomenclature that will be preserved they are ...


4

SetDirectory@NotebookDirectory[] Select[ FileNames[], DirectoryQ]



Top 50 recent answers are included