4

I am developing a plug-in (for Grafana,an open platform for analytics and monitoring, https://grafana.com/) and to make data analysis easier, I want to enable the user of the plug-in to download a .mat file directly from Grafana. However, I am not able to create a proper .mat file in my JavaScript code. I have tried the following:

<button onclick="download('file text', 'myfilename.mat', 'text/plain')"><a href="" 
id="a">click here to download your file</a>Create file</button>
<script>
    function download(text, name, type) {
        var a = document.getElementById("a");
        var file = new Blob([text], {type: type});
        a.href = URL.createObjectURL(file);
        a.download = name;
        }
</script>

From JavaScript: Create and save file. However, the .mat file that is created this way gives the errors:

Error using load Unknown text on line number 1 of ASCII file C:\Users\Esmee\Downloads\myfilename (2).mat "file".

Error in uiimport/runImportdata (line 459) datastruct = load('-ascii', fileAbsolutePath);

Error in uiimport/gatherFilePreviewData (line 427) [datastruct, textDelimiter, headerLines]= runImportdata(fileAbsolutePath, type);

Error in uiimport (line 244) gatherFilePreviewData(fileAbsolutePath);

I have no idea what to do with this. I think the problem is that simply putting the string 'file text' in a .mat file is not the file syntax that MATLAB normally uses. Does anyone have experience with creating a .mat file with Javascript/html, or does someone know in what format this file should be made in order to be readable in MATLAB?

Thanks in advance!

Esmee
  • 93
  • 11
  • Just as a side note: MATLAB can read quite a lot more stuff than .mat. Have you considered storing the data somehow else? – Ander Biguri Jan 10 '18 at 10:41
  • You can use python to [save mat-files](https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.io.savemat.html). Perhaps this can be helpful. – buzjwa Jan 10 '18 at 10:50

1 Answers1

0

MAT-files store date in binary form.

The MathWorks has published the file format specification here.

You could try to format your data (in accordance with the specification) in a single Uint8Array and pass that to the Blob constructor. See the accepted answer to this question for more details.

Brendan Cashman
  • 4,878
  • 2
  • 23
  • 20