0

Is it possible to save a file created in Matlab code to a place, specified in the filename I give the function that does the creation*, that is not in the hard drive or a flash drive, nor any other drive found in This PC (My Computer), but in the memory where the rest of the running program is found?

*such as save(filename,variable)

I'm hoping to refer to the files and then delete them as soon as the program is done, without encroaching on the user's hard drive.

After some web searching, I found this page, which solves a very similar problem in an unspecified OS as it was six years ago, but I

  • want to make sure it works in Windows 10
  • don't have Map.cmd
  • suspect that this is putting the virtual drive in his E:/ drive, not RAM

I want to make it possible to make a .exe out of my program, which currently saves files. While I'm sure I can delete them after creating them, I don't want to have the end product do anything that would alarm anyone of security risks while running this.

Post169
  • 668
  • 8
  • 26
  • You are looking for a `RAM-drive` https://en.wikipedia.org/wiki/RAM_drive. Download for example the `ImDisk Toolkit` – Gelliant Jun 28 '17 at 08:32
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – dasdingonesin Jun 28 '17 at 08:39
  • @dasdingonesin I don't think it qualifies for book/tool etc recommendation flag/cv – Sardar Usama Jun 28 '17 at 08:47
  • 1
    btw why do you want to *save* that variable in the RAM? Does MATLAB's workspace seem any different? – Sardar Usama Jun 28 '17 at 08:55
  • 1
    Why save them at all? If you want the variables to be stored in process memory, and not touch the disk, just leave them as variables. – Peter Jun 28 '17 at 12:55
  • I want to know some things about a matrix when I turn it into a PNG picture. The only way I know how to do that is with the imwrite function. Is there another? – Post169 Jun 28 '17 at 12:59
  • so you want to save an image and reload it in MATLAB? That'd be an over-kill. An image is a matrix in MATLAB. Keep it as it is ! No saving and reloading is required! – Sardar Usama Jun 30 '17 at 00:52
  • @Sardar You are assuming I want the same matrix that I saved as a picture. No, I want to know things about it that are unique to when it has been compressed by the PNG algorithm. – Post169 Jun 30 '17 at 01:18
  • Define those things – Sardar Usama Jun 30 '17 at 01:20
  • @Sardar PNG is a powerful image compression format, and I'm interested in how small it can squeeze an image into. I can use the imwrite function to turn a matrix into an image file, options including PNG, by saving it to disk. – Post169 Jun 30 '17 at 01:56
  • So your question is how to squeeze the image matrix? – Sardar Usama Jun 30 '17 at 01:58
  • If I understand you correctly, yes. And I want it done by the same algorithm used in PNG compression. – Post169 Jun 30 '17 at 01:59
  • 2
    Seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Btw doesn't the matrix remain same if you store it as a ***png*** and reload it later? It does according to [Mathworks blog](http://blogs.mathworks.com/steve/2013/05/02/jpeg-versus-png-lossy-and-lossless-image-compression/). So why do you want to save it? – Sardar Usama Jun 30 '17 at 02:22
  • Viewed from Matlab, yes, they remain the same. But while it is stored as PNG, after you've saved it and before you've reloaded it, it's a totally different bitstring. And I am focused on the PNG storage bitstring, not what it codes for. – Post169 Jun 30 '17 at 02:27
  • How are you currently retrieving that *PNG storage bitstring* in MATLAB? – Sardar Usama Jun 30 '17 at 02:34
  • 1
    I agree with @Sardar that your question sounds like the epitome of an XY problem. Or anyway saving a single png to the disk temporarily should not be a huge problem. You could even use `tempname` to generate a filename that won't bother anyone. Anyway, I tried to humour you and investigated. My idea was that python's `io.StringIO` could house a file in memory, but unfortunately `print`/`saveas` are not designed to work with these objects. I tried tracking down what `print` uses under the hood, and ended up with obfuscated p-files such as `MATLAB/toolbox/matlab/graphics/private/writeRaster.p`. – Andras Deak -- Слава Україні Jun 30 '17 at 09:39

1 Answers1

0

Perhaps you're over-thinking things? Maybe you could just save your files in the user's temporary directory. There are unlikely to be many security risks from doing that, and users are typically unalarmed by an application that does it.

If your application is written in MATLAB, you can get the name of the user's temporary directory with the command tempdir, and you can generate a random name for a temporary file with the command tempname.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64