Re: Import[...] - MAT files, unable to access variable's data via

*To*: mathgroup at smc.vnet.net*Subject*: [mg119434] Re: Import[...] - MAT files, unable to access variable's data via*From*: Albert Retey <awnl at gmx-topmail.de>*Date*: Fri, 3 Jun 2011 04:36:38 -0400 (EDT)*References*: <is958o$5ui$1@smc.vnet.net>

Am 03.06.2011 01:10, schrieb Eric Cousineau: > This seems like an easy fix, but I can't seem to get Replace[...], or > /., to substitute the variables imported from a MAT file. > > The MAT file contains two variables, 'xdata' and 'ydata'. I import > the data using `data = Import["./data.mat", "LabeledData"]` and it > yields a set of Rule's for 'xdata' and 'ydata' with their respective > data. However, when I try `xdata /. data`, it simply returns `xdata` > as a symbol. I have checked that other basic rules work, such as `2 x > /. x -> 3` returning `6`. > > Anyone know what I might be doing wrong? the import does not return symbols but strings, so: "xdata" /. data should do what you want. > I guess another question is (maybe for another post): is there a way > to inspect Rule's if replacements don't work, it is usually best to look at FullForm or at leat InputForm, which in this case would have made obvious that the rules are something like {"xdata"->{...},"ydata"->{...}} > and extract a variable's data using the variable's > name, i.e., `data[["xdata"]]`, or something thereabouts? Or would /. > be the best option? If the data is in rules as returned by Import, I think /. is your best option. You could try to find the positions of "xdata" and "ydata" with Position and then construct something that you can use like an index to access them more directly, e.g. like this: xdata=Sequence[ Flatten[Position[data, Verbatim[Rule]["xdata", _]]][[1]], 2] data[[xdata]] > I've searched around for manipulating or inspecting rules, but have > found nothing conclusive. if you look for something that lets you address data more directly you can store data in DownValues, e.g.: data["x"] = {0,1,2}; data["y"] = {2,3,4}; and then use it with: data["x"] or data@"x" hth, albert