变量名取决于导入的文件名. Matlab的 [英] Having a variable name dependant on the imported file name. Matlab

查看:259
本文介绍了变量名取决于导入的文件名. Matlab的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有72个.dat文件,可以使用以下代码上传到我的matlab工作区中;

I currently have a 72 .dat files which I can upload into my matlab workspace using the folowing code;

files = dir('*.dat');
for i=1:length(files)
    eval(['load ' files(i).name ' -ascii']);
   end

那部分工作正常.每个文件包含三列数据.第一个是时间,第二个和第三个是U和V速度分量.可以编写代码,将所有这些.dat文件导入我的工作空间,然后根据文件名命名数据的每一列.例如,每个文件一直指向Point73,依次命名为Point1,Point2等.那么从Point2.dat文件中获取时,.dat文件Point1中的三列是否可以分别命名为Time1,U1和V1以及命名为Time2,U2和V2?

That part works fine. Each file contains three columns of data. This first is time and the second and third are U and V velocity components. It is possible to write a code that will import all these .dat files into my workspace and then name each column of data with respect to the file name. For example each file is named Point1, Point2 etc all the way to Point73. So is it possible that the three columns in the .dat file Point1 can be named Time1, U1 and V1 and named Time2, U2 and V2 when taken from the Point2.dat file?

当前我正在尝试这段代码;

Currently I am trying this piece of code;

Time1  = Point1(1:1024, 1);
U1 = Point1(1:1024, 2);
V1 = Point1(1:1024, 3);
Time2  = Point2(1:1024, 1);
U2 = Point2(1:1024, 2);
V2 = Point2(1:1024, 3);

我知道这是错误的,但是我不知道如何使变量的名称依赖于原始文件名.任何帮助将不胜感激.

I know it is wrong but I don't know how to make the names of the variables dependent on the original file name. Any help would be greatly appreciated.

关于, 杰瑞

推荐答案

根据

According to Generate Field Names from Variables, you can use the bracket operator.

如果您使用结构,则可以执行以下操作:

If you use a struct, you can do like this:

for i=1:3
   varname = strcat('U',num2str(i));
   a.(varname) = i;
 end

a =

    U1 =  1
    U2 =  2
    U3 =  3

此外,出于eval ="nofollow">评估函数的替代方案. 您可以改为加载与此类似的变量:

Also, you should try to avoid eval if possible, for reasons given in Alternatives to the eval Function. You could load the variables similar to this instead:

filename = strcat('Point',num2str(i),'.dat'); % filename = Point1.dat
load(filename);

这篇关于变量名取决于导入的文件名. Matlab的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆