0
$\begingroup$

How can I easily correct this data sequence from MATLAB to one readable in Wolfram Mathematica?

The problem is the 2 outer brackets: ({{ ... }})

data = Import["C:\\Users\\sagarmatha\\OneDrive\\MATHEMATICA\\statistik\\matlabdata.mat","Data"]

({{
  {0., 5.8955}, {0.1, 3.5639}, {0.2, 2.5173}, {0.3, 1.979}, {0.4, 1.899}, 
  {0.5, 1.3938}, {0.6, 1.1359}, {0.7, 1.0096}, {0.8, 1.0343}, {0.9, 0.8435}, 
  {1., 0.6856}, {1.1, 0.61}, {1.2, 0.5392}, {1.3, 0.3946}, {1.4, 0.3903}, 
  {1.5, 0.5474}, {1.6, 0.3459}, {1.7, 0.137}, {1.8, 0.2211}, {1.9, 0.1704}, 
  {2., 0.2636}
}})
$\endgroup$
  • 1
    $\begingroup$ Is the sequence read from a file or copied/pasted from Matlab? How long is the sequence? $\endgroup$ – José Antonio Díaz Navas Jun 12 '18 at 17:03
  • $\begingroup$ I used Import[…] and it's 20 pairs $\endgroup$ – runner Jun 12 '18 at 17:13
  • 1
    $\begingroup$ Please, could you post the file to be imported so we can see its structure? $\endgroup$ – José Antonio Díaz Navas Jun 12 '18 at 17:14
  • $\begingroup$ how do I post a file? $\endgroup$ – runner Jun 12 '18 at 17:18
  • 1
    $\begingroup$ @runner thanks for the edit, it's very useful to see the code you are using. We are still missing the content of matlabdata.mat at least the first lines. $\endgroup$ – rhermans Jun 12 '18 at 18:01
2
$\begingroup$

I think this is what can be expected when importing mat files: in matlab everything is a matrix, so even when you store a single number into a mat file, it will be actually be a 1x1 matrix and imported to Mathematica as {{{1.}}}.

As you can store more than one variable into a mat file, the outermost extra bracket is just a result of Import trying to import all stored variables by default as a list. In your case there is only one variable stored so that you get a list of lenth one with the first entry being the matrix that has been stored. Matlab actually stores the variable name used in the session along with the data, so the actual file content becomes more clear whe one uses something like:

Import[filename,"LabeledData"]

To import the first variable stored in a mat file you could do this:

data = First[Import[filename]]

or make use of the possibilities to only import the first value stored like this:

data = Import[filename,{"Data",1}]

Using

Import[filename,"Labels"]

will show all variable names stored in the mat file, once you know the variable name you want to import you could also use that, like this:

data = Import[filename,{"Data","m"}]

which would import only the value for the variable named "m".

Note that matlab matrices can have depth larger than two, so you could also get matrices with larger depth with Import.

$\endgroup$

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