Tag Info

Hot answers tagged

12

I am not sure that this is possible. The $Output and $Messages variables hold the output stream to where the standard output (and the message output) from the kernel goes. If you check these, you'll see that they're simply set to stdout. If you remove ReadProtected from NotebookWrite, you'll see that it is passing data to the front end instead of writing ...


11

You could use RunScheduledTask or its relatives for this. For example, to append a random integer to catch once every two seconds you could do something like catch = {}; task = RunScheduledTask[AppendTo[catch, RandomInteger[10]], 2]; You could also use CreateScheduledTask which is similar to RunScheduledTask except that the task won't be started ...


10

Here is a function which may help: Clear[readRows]; readRows[stream_, n_] := With[{str = ReadList[stream, "String", n]}, ImportString[StringJoin[Riffle[str, "\n"]], "Table"] /; str =!= {}]; readRows[__] := $Failed; I tested on your file and it works all right (it may make sense to read rows in batches, this is much faster): n=0; str = ...


9

File Output Use an output stream. s = OpenWrite["your_filename"]; Dynamic[With[{a = ImageKeypoints[EdgeDetect[CurrentImage[], 30], "PixelPosition"]}, Write[s, a]; a], Deinitialization :> Close[s]] It will close the stream upon the deletion of the dynamic cell. It is not a bad idea to put time stamp with it. Dynamic[With[{a = ...


8

(All observations made in version 7.) There seems to be a limitation for input even in the Front End (Notebook interface), in that if I enter more than 766 levels of nested lists I get a MaxFormatDepthExceeded expression and an error beep. The help text is: A box structure with a depth exceeding the maximum allowed depth was encountered. We can at ...


7

Are you on a Windows machine? If so, try OpenReading the file with BinaryFormat->True. When you open a file on a Windows machine in "text mode," with BinaryFormat->False (which is the default), the system has to translate the Windows newline sequence \r\n (two bytes which mark the end of the line) into the single \n character. In the C language and ...


7

What happend to ABC? Input Streams "read ahead" and buffer data from their sources. When you write rtmp = OpenRead["tmp"], OpenRead reads the whole file "tmp" into memory. This is called buffering. (It reads up to 16 kilobytes, but your file's not that large.) All subsequent operations on the rtmp stream are really just operating on pre-loaded data in ...


7

Try this: stream = StringToStream[ "Apple,Jack,1,123.456\nOrange,Jill,2,456.789\n"]; While[! EndOfFile === (data = Read[stream, {Word, Word, Character, Number, Character, Number}, WordSeparators -> ","]), Print["Fruit:", data[[1]], " Name:", data[[2]], " Integer:", data[[4]], " Real:", data[[6]]];] Having learnd something from ...


6

This seems to work and doesn't require conversion stream = StringToStream[ "Apple,Jack,1,123.456\nOrange,Jill,2,456.789\n"]; While[! EndOfFile === (data = Read[stream, Riffle[{Word, Word, Number, Number}, Word], TokenWords -> {","}] /. {"," -> Sequence[]}), Print["Fruit:", data[[1]], " Name:", data[[2]], " Integer:", ...


6

The documentation for $Messages clearly states: $Messages gives the list of files and pipes to which message output is sent. Therefore Block[{$Messages = {stream}}, ... ] is the correct syntax.


6

According to the docs, both Write and WriteString should accept an "output channel," which is just a list of streams. On my system (v.8 on MacOS), Write behaves the same way as on your system when outputting anything, but WriteString behaves as expected. Similarly, this Write[{tex1, tex2}, 3, 4]; also misbehaves in the same manner. So, the issue isn't ...


6

Export["test.txt", {a}] works for me "Plaintext" is already the default output form of Export. The { } around a prevents Export from seeing it as a series of arguments (a is a list in your example) that each have to be put on its own line.


5

Streams and iterators Using streams would certainly be one of the most elegant ways to do this. In any case, you will at least need an iterator for your sequence of numbers. The reason why an abstraction of an iterator is useful is because it separates the iteration over your sequence from the stuff you want to do with individual elements, so that you can ...


5

pixpos = {}; Dynamic[ a = EdgeDetect[CurrentImage[], 30]; b = ImageKeypoints[a, "PixelPosition"]; AppendTo[pixpos, b]; a] Let it run for some time then delete the graphic to stop the Dynamic and plot them to show we indeed collected the results: Graphics@Table[{Hue[Random[]], Line[i]}, {i, pixpos}]


5

Here is a string processing way of extracting the "x-axis" data from the XPM file that you gave. I'll assume that you have already imported the all of the file's records into the string text - e.g. using text = Import[filename,"String"], though I haven't tested this as I actually copied (then pasted into an empty string) the contents of the XPM file quoted ...


5

Here is a letter I got regarding this question from premier service technical support: Thank you for taking the time to send this in. Unfortunately, I do not believe this functionality currently exists in Mathematica and I have forwarded the suggestion that it be included in a future release of Mathematica to the developers in charge of this area. ...


5

dat = Table[{i, Sin[i], Cos[i], Tan[i]}, {i, 4}] // N; Export["test.txt", dat, "Table", "FieldSeparators" -> " "] FilePrint["test.txt"] 0.8414709848078965 0.5403023058681397 1.5574077246549023 0.9092974268256817 -0.4161468365471424 -2.185039863261519 0.1411200080598672 -0.9899924966004454 -0.1425465430742778 -0.7568024953079282 ...


5

What you experience here seems to be some kind of stack limit when you have nested expressions. It doesn't seem to matter whether you nest lists or function calls. Look for instance at this example here which is nothing more than a nested call f[f[f[...f[a]]..] On the other hand, if the parser doesn't need to build up such a large stack, it seems to ...


4

I can only give a partial answer. Your problem arises because FilePrint doesn't use $Output (stdout). It uses the stderr stream, so you can't capture what it writes by using Block and assigning to $Output. Unfortunately, I don't think any system variable is bound to stderr. Perhaps I'm wrong. In that case, I hope a more knowable person will be able to ...


4

Since PrintTemporary does not use output streams, but rather is akin to CellPrint, I believe you will need to add this functionality manually. For example: Unprotect[PrintTemporary]; $log = OpenAppend["logfile.txt"]; PrintTemporary[expr_] /; ! TrueQ[ptLog] := Block[{ptLog = True}, WriteString[$log, expr]; PrintTemporary @ expr ] ...


4

OpenWrite returns the stream it opens, so do this str = OpenWrite["~/example"] OutputStream["/Users/oldmg/example", 83] Close[str] Also, all streams have a name. In this case it is "/Users/oldmg/example".


4

That's because "Base64" is not a format but an encoding and you still need to tell Mathematica what format to import after decoding from Base64. This is described in the documentation. Try this: ImportString[s, {"Base64", "String"}]


4

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.


3

You can stay with Put using the method I showed here for PutAppend: SetOptions[OpenWrite, PageWidth -> Infinity]; a >> tmp This method is especially useful in the case of PutAppend because it allows you to maintain a running log file with results of intermediate computations with one expression per line.


3

In the event you did not save the stream object for some reason, You should be able to close by the file name, ie. simply Close["~/example"]


3

Your problem is with not reassigning $Output back to stdout. You could do it by writing your code like so: fname = FileNameJoin[{HomeDirectory[], "Desktop", "test.txt"}]; Module[{out = $Output}, $Output = OpenWrite[fname, FormatType -> OutputForm]; Write[$Output, "The quick brown fox ..."]; Print[$Output]; Close[$Output]; $Output = out] The ...


3

Try using Export and Import. Make sure to include "Data" as the second argument of Import. SetDirectory[$TemporaryDirectory]; Export["myfile.txt", a] in = Import["myfile.txt", "Data"] If you really want to see the Greek letters spelled out you can then use FullForm[in] but this will also return List[] instead of {}.


3

Perhaps you could use Export instead of Put? out = StringReplace[ToString[InputForm[a]], {" " | "\n" -> ""}] Export["out.txt", out]


3

I expected this to work but it apparently only writes Print output: AppendTo[ $Output, OpenWrite["out.txt", FormatType -> OutputForm] ] A workaround using $PrePrint: output = OpenWrite["out.txt", FormatType -> OutputForm]; $PrePrint = (Write[output, #]; #) &;



Only top voted, non community-wiki answers of a minimum length are eligible