Matlab:加载文件

发布于 2024-09-18 04:58:47 字数 302 浏览 1 评论 0原文

如果我使用 matlab 的加载函数,我通常最终会做这样的事情:

temp = load('filename.mat');
realData = temp.VarName;
clear temp

或者

realData = load('filename.mat');
realData = realData.VarName;

这种方法中的任何一个都优于另一个,特别是在内存使用方面?或者是否有更直接的方法来避免这种临时结构?

谢谢 托马斯

If I use the load function by matlab I normally end up doing something like this:

temp = load('filename.mat');
realData = temp.VarName;
clear temp

or

realData = load('filename.mat');
realData = realData.VarName;

is any of this methods superiour to the other, especially in terms of memory usage? Or is there a more direct approach to avoid this temporary struct?

Thx
Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美煞众生 2024-09-25 04:58:47

如果您知道您只需要 mat 文件中的特定变量,则可以

realData = load('filename.mat', 'VarName');

参阅 Matlab有关加载命令的更多信息,请参阅文档

If you know you need just specific variables from your matfile, you can do

realData = load('filename.mat', 'VarName');

See the Matlab documentation for more info about the load command.

太傻旳人生 2024-09-25 04:58:47

好吧,如果您只是执行 load('filename.mat');,所有变量最终都会在当前范围内。

不过,我怀疑您的这两种方法是否会产生任何有意义的内存成本。 Matlab 使用写时复制。

Well, if you just do load('filename.mat');, all the variables end up in the current scope.

I doubt there's any meaningful memory cost to either of your methods, though. Matlab uses copy-on-write.

夜吻♂芭芘 2024-09-25 04:58:47

您可能想尝试使用“importdata”命令:

   szFilePath = 'c:\dirName\myData.mat';
   myData = importdata( szFilePath );

这可以避免在不带输出参数的情况下使用 load 时将变量隐式放置到作用域中,以及不必要的从结构赋值命令。

正如 Oli 所指出的,惰性复制(写时复制)行为意味着内存考虑是没有意义的。

从维护/可读性的角度来看,importdata 有几个优点:

  1. 显式命名在工作区中创建的变量可以更清楚地记录该函数正在执行的操作。
  2. 消除结构体赋值操作的必要性可以从源文件中删除分散注意力的和不相关的操作。

我使用的是 MATLAB R2010a。

You might want to try using the "importdata" command:

   szFilePath = 'c:\dirName\myData.mat';
   myData = importdata( szFilePath );

This avoids the implicit placement of variables into scope when load is used with no output arguments, as well as the unnecessary assignment-from-struct command.

As noted by Oli, the lazy copy (copy-on-write) behaviour means that memory considerations are moot.

From a maintenance/readability point of view importdata has a couple of advantages:

  1. Explicitly naming the variables that are created in the workspace documents what the function is doing much more clearly.
  2. Removing the necessity for the assignment-from-struct operation enables distracting and irrelevant operations to be removed from the source file.

I am using MATLAB R2010a.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文