我正在开发一个插件(用于 Grafana,一个用于分析和监控的开放平台,https://grafana.com/)并且为了使数据分析更容易,我想让插件的用户能够下载一个 .mat 文件直接来自格拉法纳。但是,我无法在我的 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 行的未知文本 "file"。
uiimport/runImportdata 错误(第 459 行) datastruct = load('-ascii', fileAbsolutePath);
uiimport/gatherFilePreviewData 错误(第 427 行) [datastruct, textDelimiter, headerLines]= runImportdata(fileAbsolutePath, type);
uiimport 错误(第 244 行) gatherFilePreviewData(文件绝对路径);
我不知道该怎么办。我认为问题在于,简单地将字符串“文件文本”放入 .mat 文件并不是 MATLAB 通常使用的文件语法。有没有人有使用 Javascript/html 创建 .mat 文件的经验,或者有人知道应该以什么格式制作这个文件以便在 MATLAB 中可读?
提前致谢!
最佳答案
MAT 文件以二进制形式存储日期。
MathWorks 已发布文件格式规范 here .
您可以尝试在单个 Uint8Array 中格式化您的数据(根据规范)并将其传递给 Blob constructor .查看 this question for more details 的已接受答案.
关于javascript - 使用 JavaScript 创建 .mat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48185465/