-1
$\begingroup$

In my Notebook i wrote this algorithm:

In[63]:= mData = Import["Desktop/a.txt", {"Data", {All}, {All}}];
In[64]:= mErrors = mData[[All, 3]];
In[65]:= mWeightedErrors = 1/mErrors^2;
In[66]:= Mean[WeightedData[mData[[All, {2}]], mWeightedErrors]]
Out[66]:= 16.2181
In[67]:= ErrorOfWeightedMean = Sqrt[1/Total[mWeightedErrors]]
Out[67]:= 0.0497844

and everythings works fine. Now I want to put this all in one definition, but it doesn´t work:

In[95]:=mWeightedErrorMean[filename_] := 
Module[{filename0 = filename, mData, mErrors, mWeightedErrors, 
mWeightedErrorMeanValue, mErrorOfWeightedMean},

mData = Import[filename0, {"Data", {All}, {All}}]; 
mErrors = mData[[All, 3]];
mWeightedErrors = 1/mErrors^2;
mWeightedErrorMeanValue = 
Mean[WeightedData[mData[[All, {2}]], mWeightedErrors]];
mErrorOfWeightedMean = Sqrt[1/Total[mWeightedErrors]];

Print["Der gewichtete Mittelwert ist: " mWeightedErrorMeanValue " \
und der Fehler dazu ist: " mErrorOfWeightedMean] 
]

If I try to use the function there is just an odd output:

In[96]:= mWeightedErrorMean["Desktop/a.txt"]
         0.807407 Der gewichtete Mittelwert ist:   und der Fehler dazu ist: 

What did I wrong? I am new to Mathematica, so probably something very fundamental.

$\endgroup$

closed as off-topic by Yves Klett, Öskå, Mr.Wizard May 10 '15 at 11:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Yves Klett, Öskå, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ You are missing commas in your last line: use Print["Der gewichtete Mittelwert ist: ", mWeightedErrorMeanValue ," \ und der Fehler dazu ist: ", mErrorOfWeightedMean] instead. $\endgroup$ – kglr May 10 '15 at 10:45
  • 1
    $\begingroup$ ... compare Print["xyz :" 3 " abc :" 5] versus Print["xyz :" , 3 , " abc :", 5]. Also Print is redundant here; you can use Row[{"xyz :" , 3 , " abc :", 5}] or StringForm["xyz :`` abc :``", 3, 5] or (with a=3 and b=5 in the roles of your mWeightedErrorMeanValue and mErrorOfWeightedMean ) StringJoin["xyz :" , ToString@a , " abc :", ToString@b] without the need for Print. $\endgroup$ – kglr May 10 '15 at 10:48
  • $\begingroup$ Luca, i posted the comments as an answer. $\endgroup$ – kglr May 10 '15 at 11:28
2
$\begingroup$

You are missing commas in your last line. Use

Print["Der gewichtete Mittelwert ist: ", mWeightedErrorMeanValue ,
  " \ und der Fehler dazu ist: ", mErrorOfWeightedMean]

With a=3 and b=5 in the roles of your mWeightedErrorMeanValue and mErrorOfWeightedMean, respectively, compare

Print["xyz :" a " abc :" b]

15 abc : xyz :

versus

Print["xyz :" , a , " abc :", b] 

xyz :3 abc :5

Also check

"xyz :" a "  abc :" b // Head

Times

By the way, Print is redundant here; you can use

Row[{"xyz :" , a , " abc :", b}] 

xyz :3 abc :5

or

StringForm["xyz :`` abc :``", 3, 5] 

xyz :3 abc :5

or

StringJoin["xyz :" , ToString@a , " abc :", ToString@b]

xyz :3 abc :5

without the need for Print.

$\endgroup$

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