0
$\begingroup$

What is the best way to get data from Matlab to Mathematica?

For a new project I want to copy/paste a table (or part of it) of datas from Matlab to Mathematica.

Copyed from Matlab it is:

0 0 1,00000000000000e-20 0,100000000000000 1,78810000000000e-09 0,300000000000000 2,94930000000000e-09 0,500000000000000 4,37810000000000e-09 0,700000000000000 5,58330000000000e-09 0,900000000000000 5,60570000000000e-09 1 

In Mathematica I get the following:

0 0 1, 00000000000000 e - 20 0, 100000000000000 1, 78810000000000 e - 09 0, 300000000000000 2, 94930000000000 e - 09 0, 500000000000000 4, 37810000000000 e - 09 0, 700000000000000 5, 58330000000000 e - 09 0, 900000000000000 5, 60570000000000 e - 09 1 

(In my Mathematica, there are little "x"s between the exponents and the zeros, I guess it is the columns seperator.)

So now I have to replace the "," by "." and the "e-" by "*10^-" and so on ... I could, but I don't want to do it manualy, but till now I had no success with any Mma functions. Or is there a better way to do things like this?

... in the end it should look like this:

 {{0, 0}, {1.00000000000000*10^-20, 0.100000000000000}, {1.78810000000000*10^-09, 0.300000000000000}, {2.94930000000000*10^-09, 0.500000000000000}, {4.37810000000000*10^-09, 0.700000000000000}, {5.58330000000000*10^-09, 0.900000000000000}, {5.60570000000000*10^-09, 1}} 
$\endgroup$
8
  • $\begingroup$ Related/duplicate: mathematica.stackexchange.com/q/10231/5 $\endgroup$ Commented Apr 4, 2014 at 6:56
  • $\begingroup$ @rm-rf Can't use MATLink, because it opens another instance of Matlab, so I can't access the workspace with variables I need to. $\endgroup$ Commented Apr 4, 2014 at 7:10
  • $\begingroup$ Why not use a .dat or similar file together with Import? $\endgroup$ Commented Apr 4, 2014 at 7:20
  • $\begingroup$ @Phab What's your operating system? $\endgroup$ Commented Apr 4, 2014 at 7:25
  • 1
    $\begingroup$ @Phab Ok, then please write to [email protected] or [email protected] for a possible solution. $\endgroup$ Commented Apr 4, 2014 at 7:53

1 Answer 1

3
$\begingroup$

Paste into "" and try this:

str = "0 0 1,00000000000000e-20 0,100000000000000 1,78810000000000e-09 0,300000000000000 2,94930000000000e-09 0,500000000000000 4,37810000000000e-09 0,700000000000000 5,58330000000000e-09 0,900000000000000 5,60570000000000e-09 1"; imp[s_String] := ImportString[s, "Table", "NumberPoint" -> ","] imp[str] % // ListLinePlot 

{{0, 0}, {1.*10^-20, 0.1}, {1.7881*10^-9, 0.3}, {2.9493*10^-9, 0.5}, {4.3781*10^-9, 0.7}, {5.5833*10^-9, 0.9}, {5.6057*10^-9, 1}}

Mathematica graphics

If you want all reals, just add N

imp[s_String] := ImportString[s, "Table", "NumberPoint" -> ","]//N imp[str] 

{{0., 0.}, {1.*10^-20, 0.1}, {1.7881*10^-9, 0.3}, {2.9493*10^-9, 0.5}, {4.3781*10^-9, 0.7}, {5.5833*10^-9, 0.9}, {5.6057*10^-9, 1.}}

$\endgroup$
5
  • $\begingroup$ Simple solution for copy/paste! That's what I need for the moment. I'll keep trying to find a nicer solution for importing my data with MATLink or Import. $\endgroup$ Commented Apr 4, 2014 at 8:24
  • $\begingroup$ Excellent. Perhaps better answers are on the way - it should be doable to write a menu entry to "paste as mat from clipboard" or similar, but no time right now. $\endgroup$ Commented Apr 4, 2014 at 8:25
  • $\begingroup$ Just one little problem: when importing, all numbers are reals, with your string method the zeros and ones (in the example) are integers. ... later I'm doing a replacement with data=data/.{a_Real, b_Real}:>{b, a} ... so you see the problem: it does not switch the integers. $\endgroup$ Commented Apr 4, 2014 at 8:47
  • $\begingroup$ SOLVED THE PROBLEM: dont use the heads: data=data/.{a_, b_}:>{b, a} works fine! $\endgroup$ Commented Apr 4, 2014 at 8:55
  • 1
    $\begingroup$ @Phab see my edit, all you need is N ;-) Oh, and you could use Reverse to efficiently reverse, e.g. try Reverse[{{a, b}, {c, d}}, 2]. Your replacement-based solution can be dangerous, e.g. for 2x2 matrices... $\endgroup$ Commented Apr 4, 2014 at 8:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.