Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2923

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

From Albert Retey <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Import[...] - MAT files, unable to access variable's data via
Date 2011-06-03 08:36 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <isa6eb$adl$1@smc.vnet.net> (permalink)
References <is958o$5ui$1@smc.vnet.net>

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Import[...] - MAT files, unable to access variable's data via /. (Replace[...])? Eric Cousineau <eacousineau@gmail.com> - 2011-06-02 23:10 +0000
  Re: Import[...] - MAT files, unable to access variable's data via Albert Retey <awnl@gmx-topmail.de> - 2011-06-03 08:36 +0000

csiph-web