We’re rewarding the question askers & reputations are being recalculated! Read more.

# Tag Info

30

Your problem is that you've got a slight misunderstanding of the different types of items that ReadList can read. That's OK, it can be a little confusing. To begin with: String, Number, Expression, etc. are not sub-types of Record. They are all separate types with their own rules for how they are read. The RecordSeparators option is only applied to ...

18

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 = OpenRead["C:... 16 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 ... 16 We can do it straightforward in Java using JLink: << JLink InstallJava[]; replaceBytes[file_, bytes_] := Module[{f, result}, f = JavaNew["java.io.RandomAccessFile", file, "rw"]; writeByte[ff_, {offset_, byte_}] := Module[{old}, ff@seek[offset]; old = ff@readByte[]; ff@seek[offset]; ff@write[byte]; {offset, old} ];... 16 Short Version On Windows, unlike Unix, named pipes are special and do not behave like regular stdio streams. Programs that use named pipes require custom logic. As a rule the Wolfram Language I/O functions, like most stdio programs, do not have that custom logic. Details Reading from a named pipe on Windows is a tricky business. There are all kinds of ... 13 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 ... 13 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 -0.... 12 We could get the positions of the tips of the arrows from the graphics itself, integrate to see where test points at those positions end up, and then color the arrows accordingly. Here is the code for that: plot = StreamDensityPlot[ {{3 x^2 - 6 y, 3 y^2 - 6 x}, x^3 + y^3 - 6 x y}, {x, -5, 5}, {y, -5, 5}, Epilog -> {Red, PointSize[0.03], Point[{2, 2}... 10 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 = ImageKeypoints[... 10 (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 ... 10 Similar idea to @C.E.'s, but using StreamColorFunction, which flummoxed me, since it does not work as documented for StreamDensityPlot, when the argument is of the form {vector field, scalar field}: vf2ode[vf_, vars_List] := (* vector field to ode *) D[Through[vars@t], t] == (vf /. Thread[vars -> Through[vars@t]]); (* StreamColorFunction *) ... 9 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. 9 As mentioned already it's easy to implement this functionality with JLink. But once you use Java's ZipOtputStream you will need to convert your data to list of bytes first. Borrowing java code from here. << JLink InstallJava[]; openStream[file_] := Module[{fos, bos, zos}, fos = JavaNew["java.io.FileOutputStream", file~StringJoin~".zip"]; bos = ... 8 Original answer 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. UPDATE: a bug introduced ... 8 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 ... 8 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 ... 8 I was playing around SocketLink too. (And tried implementing a "pure" Mathematica server too!). About the OP's question, on my Windows 8.1 64bit, Doing more BinaryRead[inStream] than necessary will not crash the kernel, it will just wait for input, which blocks the main link. My workaround for this is using TimeConstrained: While[True, TimeConstrained[... 8 Note: If you want this type of parser added for JSON, I think WRI could do it easily. They already have Developer`ReadRawJSONStream so I think adding some event-based parsing to that wouldn't be too much to ask This has been kicking around my for the past few days, so here's a quick example of what I discussed in my comment. It is by no means complete, but ... 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 ... 7 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 ... 7 Beginning with Mathematica 10 there is a new function ReadString to get the (current) available data from an open socket connection by setting EndOfBuffer as terminator. But there has to be at least one finished read to the stream before ReadString[socket, EndOfBuffer] will return the next chunk. Basic stream function StreamPosition, SetStreamPosition will ... 7 Yes! This is documented under Details: You can use Read to get objects to insert into any expression structure, not necessarily a list. Example: Read[stream,Hold[Expression]] gets an expression and places it inside Hold. What this means is that we can use any compound expression as the second argument of Read. Any type names (such as Number, Real, ... 7 Your ideas are too C-oriented. The Wolfram Language is not at all like C despite some syntactic sugar built into it to make some constructs C-like; In my opinion, these features more often lead newcomers astray than help them. It is better to learn to do things in the WL way, rather than trying to write code to force WL to behave like C. When working with ... 7 I have noticed that NETLink does not get a lot of love on this site (at least not as much as JLink does), so let me just post this solution based on NETLink. Note that you can use NETLink on any of the popular Operating Systems out there (.NET platform is also now open source), but if you're on Windows, NETLink is highly preferred to JLink. Anyhow, here is ... 6 Here's a way that allows you to actually redirect output to a notebook by using the newish DefineStreamOutputMethod function (which dates to 9.0). It's a bit hacky by its very nature, and the version I have here creates a new notebook for output, but I think it's a reasonable place to start. There's certainly room for refinement! DefineOutputStreamMethod["... 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

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:", data[[3]...

6

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}]

6

Summary This is generalized piece of code for reading FAST rows terminated with a newline from a CSV or TSV text stream with UTF8 encoding. You may specify options for the filetype, the starting and ending line number, as well as a string to search for a position in the file (find). By default the result includes the header which can be omitted. The ...

6

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