7
$\begingroup$

For example,in windows machine there is a variable testData

resource = ResourceObject["MNIST"];
testData = ResourceData[resource, "TestData"];

And I want to copy this variable to my linux machine.

There are two notebooks(both using Windows platform),one use local kernel(windows) and another use remote kernel(linux).

enter image description here

I try to transfer it to ByteArray then copy and paste.

Have a good idea?

enter image description here

$\endgroup$
10
  • $\begingroup$ You should look for the DistributeDefinitions or SetSharedVariable and related. These functions share the data from master-kernel to sub-kernels. $\endgroup$
    – Rom38
    Commented Sep 20, 2017 at 5:55
  • $\begingroup$ This is exactly the situation where you might want to use a MathLink between your two sessions. $\endgroup$
    – halirutan
    Commented Sep 20, 2017 at 5:58
  • $\begingroup$ @Rom38 they are all about parallel.I only want to move data $\endgroup$
    – partida
    Commented Sep 20, 2017 at 8:47
  • $\begingroup$ Perhaps you can use the Network Programming guide to create a socket connection between the two computers and transfer the data that way. $\endgroup$
    – Edmund
    Commented Sep 20, 2017 at 9:30
  • $\begingroup$ @Edmund I think you misunderstood what I mean. I'm just opening two notebooks in one computer now.I just want to copy variable from one notebook to another,don't need socket $\endgroup$
    – partida
    Commented Sep 20, 2017 at 9:33

2 Answers 2

3
$\begingroup$

As a simple solution, you could use CloudPut, CloudGet and CloudObject.

list = RandomReal[1,10];
CloudPut[list, "myRandomList"]

Then in the other kernel:

CloudGet["myRandomList"]

You need to be signed in to the same Wolfram account from both locations.


As a more direct method, you could establish a MathLink (WSTP) connection between the two kernels. This method is much more fickle, and not very tolerant to user mistakes. It also requires that the two computers be able to see each other over the network and be allowed to communicate over the ports mentioned in the link name.

Here's a short example:

enter image description here

In kernel 1,

link = LinkCreate[LinkProtocol -> "TCPIP"]

Now First[link] will give you the link name as a string. You will need this for the other kernel.

The following call will block until the connection has been established successfully.

LinkActivate[link]

Now in kernel 2,

link = LinkConnect[linkName, LinkProtocol -> "TCPIP"]

linkName is what you got from kernel 1. It consists of port1@ip,port2@ip. Make sure that the IP address is written in a format that can be used on this second computer to reach the first (i.e. not 127.0.0.1).

LinkActivate[link]

At this point, the LinkActivate call should return on kernel 1 as well.

Now you can send data. On one kernel, LinkWrite[link, data], then on the other kernel, LinkRead[link].

Reference:

$\endgroup$
3
  • $\begingroup$ Easier than the CloudPut setup might be to create a ChannelObject. I've passed data from a cloud session to local session like this as a proof of concept. $\endgroup$
    – b3m2a1
    Commented Sep 20, 2017 at 15:44
  • $\begingroup$ It's a possibility ... but I think we have differing concepts of "easy". $\endgroup$
    – Szabolcs
    Commented Sep 20, 2017 at 15:46
  • $\begingroup$ There also are CloudSymbol["myVar"] and PersistentValue["myVar", "Cloud"] which seem to be designed specifically for this task. $\endgroup$ Commented Sep 22, 2017 at 1:40
3
$\begingroup$

You could encode data into an Image:

(* NOTEBOOK 1 *)

encode = Image[{ToCharacterCode @ Compress @ #}, ImageSize -> {100, 100}] &;

testData = ExampleData[{"Text", "DeclarationOfIndependence"}];

encode[testData]   (* copy apparently blank output image *)


(* NOTEBOOK 2 *)

decode = Uncompress @ FromCharacterCode @ First @ Round @ ImageData @ # &;

(* paste image *) // decode

When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume, among the Powers ...

$\endgroup$
2
  • 1
    $\begingroup$ If copying from one notebook and pasting to another is an option, I like the SaveToCell function here: szhorvat.net/pelican/save-data-in-notebooks.html $\endgroup$
    – Jason B.
    Commented Sep 20, 2017 at 21:11
  • $\begingroup$ @JasonB. interesting article $\endgroup$
    – partida
    Commented Sep 21, 2017 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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