Tag Info

New answers tagged

3

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]


2

table = {{10, 2}, {3, 40}}; export = (Transpose@IntegerString@table)[[#]] /. a_String :> Table[" ", {# + 2 - StringLength@a}] <> a & /@ {1, 2} // Transpose; Export["file.dat", export, "Table"]


3

One way would be to do the padding manually: padToWidth[width_, n_] := StringJoin@PadLeft[Characters@ToString[n], width, " "] newData = MapAt[padToWidth[4, #] &, data, {All, 1}]; (* Apply to first column *) newData = MapAt[padToWidth[3, #] &, newData, {All, 2}]; (* Apply to second column *) Export["data.dat", StringJoin[First@#, " ", Last@#] & ...



Top 50 recent answers are included