Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The purpose of the code below is to import data from several .txt files and then plot it using ListPlot. The data files are .txt files with comma-separated data (many rows of data in each file).

In the code below, up to four data files are each selected individually using FileNameSetter. The are selected individually since they may reside in different locations.

I have noticed that the program works sometimes, but it doesn't work sometimes and I am not sure why.

When it works, the "data" variable (defined in the DynamicModule) is filled with 1 to 4 lists of data, coming from the 1 to 4 data files that have been selected before pressing "Import data" button (Button control at the bottom of the code). In some cases, however, the code will work for if 2 data files are selected, but it will not work (i.e. "data" variable is not filled with data) when 3 or 4 data files are selected.

Can you see where the problem is?

DynamicModule[
 {calcAll, data, file1 = "", file2 = "", file3 = "", file4 = "", 
  numRows1 = 15, numRows2 = 15, numRows3 = 15, numRows4 = 15, 
  startRow1 = 1, startRow2 = 1, startRow3 = 1, startRow4 = 1, 
  xVariable = 6, yVariable = 2, yVariable2 = 0, fileNames = {}},
 (*Description of variables*)
 (*
 calcAll: function to import data from .txt files,
 data: list of data imported,
 filen: file name for nth data file (n=1 to 4),
 numRowsn: number of rows of data to be read from filen (n=1 to 4),
 startRown: starting position to read data from file n (n=1 to 4),
 xVariable: column index (i.e. column number) for xVariable (same for \
all data files),
 yVariable: column index (i.e. column number) for yVariable (same for \
all data files),
 yVariable2: column index (i.e. column number) for yVariable2 (same \
for all data files)
 *)

 (*getData: function to import data from fileName*)
 getData[fileName_ /; fileName != "",
   startRow_ /; IntegerQ[startRow],
   endRow_ /; IntegerQ[endRow],
   xvar_ /; IntegerQ[xvar] && xvar > 0,
   yvar_ /; IntegerQ[yvar] && yvar > 0,
   yvar2_ /; IntegerQ[yvar2] && yvar2 >= 0] :=
  If[yvar2 == 0, 
   Import[fileName, {"Data", {All}, {xvar, yvar}}][[
    startRow ;; endRow, All]], 
   Import[fileName, {"Data", {All}, {xvar, yvar, yvar2}}][[
    startRow ;; endRow, All]]];

 (*calcAll: function to import data (and later to plot using ListPlot \
*)
 calcAll[] := (
   data = MapThread[getData[#1, #2, #3, #4, #5, #6] &,
     {
      {file1, file2, file3, file4},
      {startRow1, startRow2, startRow3, startRow4},
      {startRow1 + numRows1, startRow2 + numRows2, 
       startRow3 + numRows3, startRow4 + numRows4},
      {xVariable, xVariable, xVariable, xVariable},
      {yVariable, yVariable, yVariable, yVariable},
      {yVariable2, yVariable2, yVariable2, yVariable2}}];
   dataToPlot = 
    Select[data, ! StringMatchQ[ToString[#], "*getData*"] &];
   );


 (*Initialize*)
 calcAll[];


 Column[{
   (*user input*)
   Grid[{
     {"Variable", "Description", "Column in \r data file"},
     {"x variable", 
      PopupMenu[Dynamic[xVariable], {6 -> "Length", 1 -> "Time"}], 
      Dynamic[xVariable]},
     {"y variable", 
      PopupMenu[
       Dynamic[yVariable], {2 -> "Temp actual", 3 -> "Temp setpoint", 
        4 -> "Output power"}], Dynamic[yVariable]},
     {"y variable #2", 
      PopupMenu[
       Dynamic[yVariable2], {0 -> "", 2 -> "Temp actual", 
        3 -> "Temp setpoint", 4 -> "Output power"}], 
      Dynamic[yVariable2]},
     {Spacer[{1, 20}]},
     {"Select data"},
     {"Browse to \r select file", "Filename", "Start row", 
      "Number rows \r to plot"},
     {FileNameSetter[Dynamic[file1], "Open"], 
      Dynamic[FileNameTake[file1]], 
      InputField[Dynamic[startRow1], FieldSize -> 5], 
      InputField[Dynamic[numRows1], FieldSize -> 5]},
     {FileNameSetter[Dynamic[file2], "Open"], 
      Dynamic[FileNameTake[file2]], 
      InputField[Dynamic[startRow2], FieldSize -> 5], 
      InputField[Dynamic[numRows2], FieldSize -> 5]},
     {FileNameSetter[Dynamic[file3], "Open"], 
      Dynamic[FileNameTake[file3]], 
      InputField[Dynamic[startRow3], FieldSize -> 5], 
      InputField[Dynamic[numRows3], FieldSize -> 5]},
     {FileNameSetter[Dynamic[file4], "Open"], 
      Dynamic[FileNameTake[file4]], 
      InputField[Dynamic[startRow4], FieldSize -> 5], 
      InputField[Dynamic[numRows4], FieldSize -> 5]},
     {Button["Import data \r", calcAll[], 
       Background -> RGBColor[1.`, 0.843104`, 0.`]]}
     }]

   (*output*)
   , Spacer[{1, 20}]
   , Row[{"Results"}]
   , Row[{"data: ", Dynamic[data]}]


   }](*Column*)

 ](*DynamicModule*)
share|improve this question
Problems of sporadic failure of dynamic code are sometime fixed by adding "SynchronousUpdating->False" – m_goldberg Jan 4 at 5:49
m_goldberg, Thank you...I see that if I wrap the whole DynamicModule[...] code in Style[...,DynamicEvaluationTimeout->20] then it seems to work correctly. – user5272 Jan 4 at 6:08

closed as too localized by Ajasja, Oleksandr R., Sjoerd C. de Vries, rm -rf Jan 23 at 5:00

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.