# Debuging in Mathematica [duplicate]

## Description

I have using Mathematica to solve all kinds of problems that encountered in my study. However, the function that I define to solve specific problem is not always right, in other words, it will occur all kinds of unexpected error. So I think I can debug it by looking over middle values of function's running process.

## Example

I write a functionGridLineDatato solve my data.(the ultimate right edition as below, but I takes much time to debug it many times)

GridLineData[data_List, Δ_] := Block[
{sortData, length, OriginData, deleteData},
sortData = DeleteDuplicates@(Sort@data);
length = Length@sortData;
OriginData =
Append[
Prepend[sortData, 2 sortData[[1]] - sortData[[2]]],
2 sortData[[length]] - sortData[[length - 1]]];
deleteData =
List /@ (First /@ Select[
#[[2]] < Δ &]) + 1;
Delete[Total@#/2 & /@ Partition[OriginData, 2, 1], deleteData]
]


Using this fuction

DebugData =
{{0.349661, 0.380297}, {0.858156, 0.8442}, {0.906745, 0.171579},
{0.0847783, 0.277227}, {0.198453, 0.40206}, {0.941614, 0.347187}};
GridLineData[DebugData[[All, 1]], .1]
(*==>{0.027941, 0.141616, 0.274057, 0.603909, 0.959049}*)


## Question

When I debug this function, I always want to know its middle value in running process. I use a tedious method that output the middle values as the return result of this function. So the function can be rewritten as below:

  GridLineData[data_List, Δ_] := Block[
{sortData, length, OriginData, deleteData},
sortData = DeleteDuplicates@(Sort@data);
length = Length@sortData;
OriginData =
Append[
Prepend[sortData, 2 sortData[[1]] - sortData[[2]]],
2 sortData[[length]] - sortData[[length - 1]]];
deleteData =
List /@ (First /@ Select[
#[[2]] < Δ &]) + 1;
{sortData,OriginData,deleteData,
Delete[Total@#/2 & /@ Partition[OriginData, 2, 1], deleteData]}
]


Rerun the function that give middle values

GridLineData[DebugData[[All, 1]], .1]
(**==>{{0.0847783, 0.198453, 0.349661, 0.858156, 0.906745,
0.941614}, {-0.0288964, 0.0847783, 0.198453, 0.349661, 0.858156,
0.906745, 0.941614, 0.976483}, {{5}, {6}}, {0.027941, 0.141616,
0.274057, 0.603909, 0.959049}})


I know the Mathematica has the inner function Trace, but it gives complex transform process. So my question is How to extract middle value to debug in Mathematica enviroment.

• Have you tried the varieties of Monitor, such as Monitor, StepMonitor, and EvaluationMonitor? Maybe those will help you. Jul 5, 2014 at 1:52
• @seismatica,No,I've never heard of Monitor, StepMonitor, and EvaluationMonitor Jul 5, 2014 at 1:54
• Time to try em out ;) Jul 5, 2014 at 2:00
• @seismatica,+1,OK,I will have a try!Thanks Jul 5, 2014 at 2:08
• Also see Reap and Sow Jul 5, 2014 at 3:15