使用JavaScript创建.mat文件

时间:2018-01-10 10:38:02

标签: javascript html matlab export

我正在开发一个插件(对于Grafana,一个用于分析和监控的开放平台,https://grafana.com/)并且为了使数据分析更容易,我想让插件的用户下载一个。 mat文件直接来自Grafana。但是,我无法在JavaScript代码中创建正确的.mat文件。我尝试过以下方法:

<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>

来自JavaScript: Create and save file。但是,以这种方式创建的.mat文件会产生错误:

使用加载时出错 ASCII文件C:\ Users \ Esmee \ Downloads \ myfilename(2).mat的第1行的未知文本 “文件”。

uimport / runImportdata出错(第459行)                     datastruct = load(' - ascii',fileAbsolutePath);

uiimport / gatherFilePreviewData出错(第427行)         [datastruct,textDelimiter,headerLines] = runImportdata(fileAbsolutePath,type);

uimport出错(第244行)     gatherFilePreviewData(fileAbsolutePath);

我不知道该怎么做。我认为问题在于简单地将字符串'file text'放在.mat文件中并不是MATLAB通常使用的文件语法。有没有人有使用Javascript / html创建.mat文件的经验,或者有人知道应该以什么格式制作这个文件以便在MATLAB中可读?

提前致谢!

1 个答案:

答案 0 :(得分:0)

MAT文件以二进制形式存储日期。

MathWorks已发布文件格式规范here

您可以尝试在单个Uint8Array中格式化数据(根据规范)并将其传递给Blob constructor。请参阅this question for more details的接受答案。