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


Groups > comp.soft-sys.math.mathematica > #2902 > unrolled thread

Import[...] - MAT files, unable to access variable's data via /. (Replace[...])?

Started byEric Cousineau <eacousineau@gmail.com>
First post2011-06-02 23:10 +0000
Last post2011-06-03 08:36 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#2902 — Import[...] - MAT files, unable to access variable's data via /. (Replace[...])?

FromEric Cousineau <eacousineau@gmail.com>
Date2011-06-02 23:10 +0000
SubjectImport[...] - MAT files, unable to access variable's data via /. (Replace[...])?
Message-ID<is958o$5ui$1@smc.vnet.net>
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?

P.S. I am able to access the data via `data[[1,2]]`, but this is hackish and I can't guarantee the order of the variables.

I guess another question is (maybe for another post): is there a way to inspect Rule's and extract a variable's data using the variable's name, i.e., `data[["xdata"]]`, or something thereabouts? Or would /. be the best option?

I've searched around for manipulating or inspecting rules, but have found nothing conclusive.

Thanks and Gig 'em!
- Eric

[toc] | [next] | [standalone]


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

FromAlbert Retey <awnl@gmx-topmail.de>
Date2011-06-03 08:36 +0000
SubjectRe: Import[...] - MAT files, unable to access variable's data via
Message-ID<isa6eb$adl$1@smc.vnet.net>
In reply to#2902
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web