matlab - `eval()`可以避免吗? (运行时指定变量名)

标签 matlab eval

我编写了一个脚本,可以省去我在 Matlab 中手动导入测试期间记录的数据的麻烦。

每次测试运行都会在 .csv 文件中保存大约 2600 个变量,每个变量都有 2 个标题行、两列数据和 ; 是分隔符。

文件名来自监控程序使用的内部 C 结构,因此属于以下类型:foo.bar.another.foo.bar.local_varname#VALUE.csv 我想要使用它在 Matlab 中重新创建结构,以便仅将其保存在 test_name.mat 文件中。

很多时候 local_varname 的长度超过 63 个字符,因此我有一些替换规则,以便缩短名称而不让 Matlab 截断名称(因此试图避免命名冲突)。

这是代码

clear all
clc

% Main names
path_self         = pwd;
backslash_indices = strfind(path_self,'\');
test_name         = path_self(backslash_indices(end)+1:end); % the directory name gives me the test_name

% Preallocation
filenames = cell(1,2600);
addresses = cell(1,2600);
i=0;

% Full list
MyFiles = dir(path_self); 

% Discard subdirectories and non interesting files
for k=1:length(MyFiles)
    if ~MyFiles(k).isdir,
        if ~isempty(strfind(MyFiles(k).name,'#VALUE.csv'))
            i=i+1;
            % Too many files
            if i > length(filenames)
                filenames = [filenames cell(1,100)];
                addresses = [addresses cell(1,100)];
            end
            % Naming Substitution Rules

            %%% INSERT HERE BUNCH OF RULES

            % Addresses and names
            filenames{i} = strrep(filename,'#VALUE.csv','');
            addresses{i} = fullfile(path_self, MyFiles(k).name);
        end
    end
end
filenames = filenames(1:i);
addresses = addresses(1:i);

% Check that no conflicts are created
if length(filenames) ~= length(unique(filenames))
    error('filenames not unique')
end

% Housekeeping #1
clear MyFiles backslash_indices i k path_self

% Import data
for j=1:length(filenames)
    % Read data
    Data = importdata(addresses{j}, ';', 2);
    % Assign data
    eval([filenames{j}, '.time   = Data.data(:,1)./1000000;']); % Converted in seconds
    eval([filenames{j}, '.values = Data.data(:,2);']);
    % Let's avoid data corruption
    clear Data
end

% Housekeeping #2
clear filenames addresses j 

% Save data struct
save(test_name, '-regexp', '^((?!name).)*$')

现在是我的问题 在研究信息并帮助编写上述代码时,我经常发现人们对使用 eval() 皱眉:为什么会这样?遇到上述情况,我能避免吗?

谢谢

编辑 正如 @wakjah 所建议的,我测试了 containers.Map() 方法。不幸的是,它不适合我们的需求,因为此时需要一个键列表,并且访问数据并不完全友好(请记住,我有大约 2600 个变量,这意味着至少有相同数量的键)

至于@Dennis Jaheruddin 的要求,数据结构是可用的,并且不会产生任何类型的冲突,即使使用这些长变量名(假设两个连续 之间的每个名称*。长度少于 63 个字符)

*我很抱歉没有使用更好的技术术语

最佳答案

来自this Mathworks 页面:

Although the eval function is very powerful and flexible, it not always the best solution to a programming problem. Code that calls eval is often less efficient and more difficult to read and debug than code that uses other functions or language constructs. ...

您可以轻松地使用括号符号来完成您的任务。一个简单的例子:

s = struct();
myFieldName = 'test';
s.(myFieldName) = myFieldValue;

这会将结构 s 中的 test 字段设置为 myFieldValue

还有this Loren 就这个主题发表的博客文章。

编辑:由于您的要求是字段名称长度超过 63 个字符,因此另一种方法是使用 containers.Map 对象。这是一个小例子:

>> m = containers.Map();
>> myFieldName = repmat('abcdefg', [1 10]); % 70 chars long
>> m(myFieldName) = 12345; 
>> m(myFieldName)

ans =

       12345

关于matlab - `eval()`可以避免吗? (运行时指定变量名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15681881/

相关文章:

matlab - 如何将元胞数组中不同大小的矩阵组合成MATLAB中的矩阵

reactjs - 如何在 ReactJS 中渲染存储为字符串的 jsx

java - 在 Eval () 中执行 toString() - Groovy(Gstring 中的方法调用)

matlab - MatLab 中的矩形函数/方波

python - python 中的 eval 和 exec 有什么区别?

python - eval、exec 和 compile 之间有什么区别?

r - 是否有更有效/更干净的方法来设置 eval(parse(paste0( 设置?

matlab - 理解matlab中的循环

c# - C#中的低通和高通滤波器

matlab - 通过MATLAB中的fft进行卷积定理