0
$\begingroup$

I know this must be a very basic question, but I could not find any answers. I need to create a function that: 1) imports data from an Excel Spreadsheet, 2) assign specific information from this xlsx file to variables and 3) plot this variables.

I wrote the function like this:

crindv[a_] := ({dados = Import[a]};{crpluscond= a[[1, Range[1,19], 2]], crminuscond = a[[1, Range[1,19], 3]]}; ListLinePlot[{crpluscond, crminuscond}, PlotMarkers->{}, PlotLegends->{"CS+", "CS-"}, FrameLabel->{"Condicionamento", "SCR (em \[Mu]S)"},Frame -> {True, True, False, False}, PlotRange->{{0,20}, {0, 1.5}}, FrameTicks-> {False, True}])

When I call this function,

In[36]:= crindv["/Users/pedrofonsecazuccolo/Dropbox/Doutorado/Piloto2/planejamento_e_analise/dados_para_mathematica/camila_mat.xlsx"]

the following error appears and Mathematica produces an empty graph:

During evaluation of In[36]:= Part::partd: Part specification /Users/pedrofonsecazuccolo/Dropbox/Doutorado/Piloto2/planejamento_e_analise/dados_para_mathematica/camila_mat.xlsx[[1,{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19},2]] is longer than depth of object. >>

During evaluation of In[36]:= Part::partd: Part specification /Users/pedrofonsecazuccolo/Dropbox/Doutorado/Piloto2/planejamento_e_analise/dados_para_mathematica/camila_mat.xlsx[[1,{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19},3]] is longer than depth of object. >>

I don't understand what is going wrong, since I am able to plot the same data by tipping the commands separately, like this:

In[27]:= dados = Import["/Users/pedrofonsecazuccolo/Dropbox/Doutorado/Piloto2/planejamento_e_analise/dados_para_mathematica/camila_mat.xlsx"];

In[28]:= Dimensions[dados]

Out[28]= {1,19,16}

In[32]:= crpluscond = dados[[1, Range[1,19],2]];

crminuscond = dados[[1, Range[1,19],3]];

In[34]:= ListLinePlot[{crpluscond, crminuscond}, PlotMarkers->{}, PlotLegends->{"CS+", "CS-"}, FrameLabel->{"Condicionamento", "SCR (em \[Mu]S)"},Frame -> {True, True, False, False}, PlotRange->{{0,20}, {0, 1.5}}, FrameTicks-> {False, True}];

Why does Mathematica not recognize the same dimensions of the data when it is imported using my function?

Thanks

$\endgroup$
2
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Mar 31, 2015 at 16:12
  • 1
    $\begingroup$ a is the name of the Excel file not the data in the Excel file. try change a to dados $\endgroup$ Mar 31, 2015 at 16:14

1 Answer 1

1
$\begingroup$

In your first line of your code the variable a is assigned the string ["/Users/pedrofonsecazuccolo...." and then trying to use the Part function that is a[[1,Range[1,19],2]] which indeed is a wrong part specification for the string.

Probably you intended dados[[1,Range[1,19],2]], which would take a Part from the data retrieved by Import[a] and assigned to dados.

OK?

Following your commments you may make following checks:

a) Did your replace in both assignments a with dados for crpluscond and for crminuscond ?

b) consider syntax for assigning the two variables crpluscond, and crminuscond as below

c) {dados=Import[a]} adds a depth to the array, not sure if required or not, you may test removing the list brackets {}.

crindv[a_] := ({dados = Import[a]}; 
  crpluscond = dados[[1, Range[1, 19], 2]]; 
  crminuscond = dados[[1, Range[1, 19], 3]]; 
  ListLinePlot[{crpluscond, crminuscond}, PlotMarkers -> {}, 
   PlotLegends -> {"CS+", "CS-"}, 
   FrameLabel -> {"Condicionamento", "SCR (em \[Mu]S)"}, 
   Frame -> {True, True, False, False}, 
   PlotRange -> {{0, 20}, {0, 1.5}}, FrameTicks -> {False, True}])

PS: if these hints help you in finding a solution don't forget to vote for the penguins....

$\endgroup$
3
  • $\begingroup$ I have changed a to dados following @Algohi and your comment and now Mathematica will not return an error message. However, my function will not return any plot. When I call crindv["/Users/pedrofonsecazuccolo...."], Mathematica will return only out = crindv["/Users/pedrofonsecazuccolo...."]. Is there something else I should have added to my function code? $\endgroup$ Apr 1, 2015 at 13:22
  • $\begingroup$ I corrected the synthax and now the function is working. Thank you for your help. $\endgroup$ Apr 2, 2015 at 13:58
  • $\begingroup$ PS: I tried, but it appears I cannot vote because of my low reputation. $\endgroup$ Apr 2, 2015 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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