Hot answers tagged output
12
The output from Information[] gets sent to the $Urgent stream. Here is how you can capture it:
infoFile = OpenWrite["info.txt", FormatType -> OutputForm, PageWidth -> Infinity]
Block[{$Urgent = infoFile},
Information["DSolve", LongForm -> False]
]
Close[infoFile]
Note that I used PageWidth->Infinity. This results in possibly (?) cleaner ...
10
The question is, what do you want to do with the output. The output of Position is in a form so that it can directly be used with Extract
list = {a, b, a, a, b, c, b};
pos = Position[list, b];
Extract[list, pos]
(* {b, b, b} *)
For this simple example, it is a bit useless because we already know, that on all positions pos we have a b in list. ...
9
I consider this a bug in the front end. Very odd it is, that not all forms eat up the first result. Consider this simple example
m = {1};
MatrixForm@m
MatrixForm@m
which gives 2 outputs as expected. If we look on the traffic between kernel and front end, then we see, that the kernel indeed sends 2 outputs back. No matter which kind of form we use:
FE ...
8
There are several issues here. You need to "inject" the symbol name into the expression using With (or similar) to prevent trying to make an assignment to ToExpression["col" <> ToString[i]]. Further, you've got spurious Background -> expressions which do not belong. (I also use Symbol in place of ToExpression.) That gives us:
...
6
I'll assume that you want to echo any literal Set operation that occurs in input, even if it is not on a line by itself.
$Pre
This may work for you:
$Pre =
Function[
main,
Unevaluated[main] /. Set -> Function[, Print@HoldForm[# = #2]; # = #2, HoldFirst],
HoldAll
];
Now:
{a = 2 + 2, b = 10/2, c = Sqrt[9]};
a = 4
b = 5
c = 3
...
5
One way to do this is to define the solution as
sol = Solve[b^2 + b*z + 1 == 0, b][[1]];
and then plot using the strategy suggested in the Help file for Solve, which replaces the variable in the rule (in this case b) with the desired solution:
Plot[b /. sol, {z, -10, 10}, PlotRange -> All]
5
It's just not always true that $(R^3)^{1/3} = R$. How about $R=i$, for example?
N[(I^3)^(1/3)]
(* Out: 0.866025 - 0.5 I *)
If you expect this, you might have more luck with the real-valued CubeRoot function. For example:
FullSimplify[CubeRoot[R^3]]
(* Out: R *)
5
The following works for me
output = OpenWrite["C:\\Users\\Mike Croucher\\outtest3.txt", FormatType -> OutputForm];
$Output = output
Print["hello"]
Close[output];
I get the word 'hello' in outtest3.txt. However, the following does not work
output = OpenWrite["C:\\Users\\Mike Croucher\\outtest4.txt", FormatType -> OutputForm];
$Output = output
...
4
Presuming you only want to this special output to come from computations that bind a variable to the value of the computation, here is one way it can be done by $Pre and $Post:
SetAttributes[saveSet, HoldAll];
saveSet[form : Set[var_, _]] := (lastSet = ToString@Unevaluated@var; form);
saveSet[form : ___] := (lastSet =.; form)
$Pre = saveSet;
$Post = ...
4
I have found one solution, using a temporary file:-
streams = AppendTo[$Output, OpenWrite[]];
Module[{},
Print[Plot[Sin[x], {x, 0, 2 Pi}]];
a = 123];
Close@Last@streams;
$Output = Most@streams;
printoutput = ReadList@First@Last@streams
3
You can temporarily redefine Print, like so:
fun[] := Module[{}, Print[Plot[Sin[x], {x, 0, 2 Pi}]];
a = 123]
list = {};
Block[{Print = AppendTo[list, {##}] &}, fun[]]
Now list contains everything that was printed. (Of course in a practical application you'd probably want to do something smarter than an inefficient periodic AppendTo)
If you still ...
3
Why not this?
x = {1, 2, 3};
y = {4, 5, 6};
z = {7, 8, 9};
output = Table[x[[i]] + y[[j]] + z[[k]], {i, 1, 3}, {j, 1, 3}, {k, 1, 3}]
sfile = OpenWrite["test.txt"]
For[i = 1, i <= 3, i++, For[j = 1, j <= 3, j++, For[k = 1, k <= 3, k++,
str = ToString[{x[[i]], y[[j]], z[[k]], output[[i, j, k]]}];
WriteString[sfile, StringReplace[str, "{" | "}" ...
3
sfile = OpenWrite["~/Desktop/test.txt", FormatType -> StandardForm]
For[i = 1, i <= 3, i++,
For[j = 1, j <= 3, j++,
For[k = 1, k <= 3, k++,
Write[sfile, x[[i]], " ", y[[j]], " ", z[[k]], " ",
output[[i, j, k]]]]]]
Close[sfile]
seems to work. I used FormatType -> StandardForm and added the spaces by hand. I'm sure there are ...
3
A self-destructing cell that creates a self-destructing button which deletes all cells generated by Print:
(credit: Sasha, jVincent and Yves Klett for the ideas in answers/comments in the linked Q/As)
Print[Button["Delete Print-generated cells & disappear",
NotebookFind[SelectedNotebook[], "Print", All, CellStyle]; NotebookDelete[]]];
...
3
I suspect that this has something to do with the code that produces the InputForm cell tags, or rather tagged cells. In addition to the last tagged cell replacing the prior ones you can observe strange behavior when combining CellPrint and InputForm:
InputForm[1 // CellPrint]
InputForm[2 // CellPrint]
InputForm[3 // CellPrint]
1
Out[1]//InputForm= 2
...
3
When handling symbols like you do, I find it best to avoid converting from and to strings. This is typically needed in other languages to do meta-programming, however due to Mathematicas philosophy being that everything is an expression, meta-programming can be accomplished simply by holding evaluation order. Here is a simple implementation of your ...
2
You print by having a Print[] on the expression you want to print. End all other statements with ;.
fooToPrint = bar[...]; Print[fooToPrint];
fooNoPrint = bar[...];
e.g.,
a = 1 + 2;
Print[a];
b = 3 + 4;
a = b;
Print[a];
2
You can also use FlipView
Row[Table[FlipView[Style["x", 50, Background -> #] & /@ {Gray, Green, Red}], {5}]]
2
Something along these lines?
SetAttributes[f, HoldAll];
f /: h_?headPredQ[b___, f[arg_], a___] :=
With[{res = h[b, arg, a]}, res /; Hold[res] =!= Hold[h[b, arg, a]]];
Format[f[arg_]] := Defer[arg];
headPredQ[h_] := MemberQ[Attributes@h, NumericFunction];
EDIT
New version that contemplates the third case I had forgot (thanks @MichaelE2). This compares ...
1
I think the answer is that the main link has to evaluate the Get call. Reusing Mr.Wizards test:
Export["testfile.m", "Print[2+2]; f[x_]:=Sin[x]", "String"]
Button["Load", Get["testfile.m"], Method -> "Queued"]
works for me. For an explanation what I mean with main link you could check the chat log. I don't know whether it is good practice to include ...
1
I think the simplest and most flexible method is to just add print statements before each operation that you want to label:
Print["myVariable ="];
myVariable=1+1
myVariable =
2
Print["{M, r, T} ="];
{m=10,r=6/2,t=1/2}
{M, r, T} =
{10,3,1/2}
This method allows you to label expressions without the limitations of variable naming in ...
1
Revisiting this question after noticing the third requirement of the question and the statement that you need this only for one function I believe you may want this:
SetAttributes[f, HoldFirst]
Format[f[x_]] := Defer[x]
f /: Times[f[x_], y_?NumericQ, z___] := (x*y) z
Now:
f[1 - 1/2]
Times[f[1 - 1/2], 5]
Times[f[1 - 1/2], x]
1 - 1/2
5/2
x (1 - ...
1
I like how Rojo's answer tests for evaluation in general, but it requires an additional fix as noted by Michael E2. So here is another approach where I interpret "evaluating" in the question as meaning "to yield a numerical result". This can be tested with NumericQ. Then I just have to take care of the non-numeric case by returning the Defered result and ...
Only top voted, non community-wiki answers of a minimum length are eligible


