1
$\begingroup$
f::args="`1` is not a prorper table.";

SetAttributes[f,HoldAll]

f[tab_/;ListQ[ReleaseHold@tab]===False]:=Message[f::args,HoldForm@tab]

f[tab_]:=ListPlot[ReleaseHold@tab]

g=Table[{i,i^2},{i,10}];

In this code I want to ListPlot only proprly defined tables. If I run f[g[[2;;,{1,2}]]] this gives expected plot. But if I by mistake type f[g[[2;;{1,2}]]] this gives error message from Part and also my message from f, but I want in this error case to see only message from f and no messages from Mathematica functions. How to make f to behave this way?

Thanks.

$\endgroup$
2
$\begingroup$

Let's assume you have a good reason to for HoldAll attribute even though your example does not need it.

Before I answer let's take a look at your code:

  • you don't need to use ReleaseHold because there is no explicit Hold there, the argument is held and using Condition or PatternTest will result it being evaluated anyway.

  • ListQ should return True or False so ! ListQ[...] is more idiomatic than ListQ[..]===False]

So your original example can be rewritten as:

f[tab_ /; !ListQ[tab] ]:=Message[f::args, HoldForm@tab]

Now, if you want to suppress other messages you can use Quiet. Those messages are issued when you are checking the condition so you can do:

 f[tab_ /; Quiet[!ListQ[tab]] ]:=Message[f::args, HoldForm@tab]
$\endgroup$
  • $\begingroup$ Thanks for the answer! Actually I have a function of two arguments (two tables, or lists) that I want to ListPlot. The function's body is Manipulate with InputFields for the input arguments, and other controls (ranges, labels, grids etc.). One possibility is using InputField[..., String] and then ToExpression@ in ListPlot. I decided to try another idea of using HoldAll to suppress evaluation arguments in corresponding InputFields. So then I need ReleaseHold inside Manipulate, and I used this in my simple exmaple. Your suggestion works well, thank you! $\endgroup$ – Alx Jul 16 at 13:37
  • $\begingroup$ But now I don't get how to make smth like this for checking arguments of InputField. If one dcides to edit initial tables from function input (in InputField) and makes this kind of typo--- one gets myriad error messages from Mathematica ... $\endgroup$ – Alx Jul 16 at 13:41
  • $\begingroup$ Well, I can add Enabled->False to InputField to prevent erroneous editing. $\endgroup$ – Alx Jul 16 at 14:23
  • $\begingroup$ @Sam_1q I find it hard to grasp your problem only from this description but I've seen your next question so let me take a look. $\endgroup$ – Kuba Jul 17 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.