评估外部文件字符串中的变量 [英] Evaluate variables in external file strings

查看:55
本文介绍了评估外部文件字符串中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想导入包含数字和变量/符号混合数据类型的CSV文件.导入部分已经在导入具有混合数据类型的CSV文件中进行了讨论.

I want to import a CSV file with mixed data types of numbers and variables/symbols. The import part was already discussed in Import CSV file with mixed data types.

此处的CSV文件仅包含2x2公式(但通常为64x64,我对其进行了1'000'000次评估). 我面临的问题是细胞的评估.不能以相同的方式完成此操作.

The CSV file here contains only 2x2 formulas (but in general 64x64 and I evaluate it 1'000'000 times). The problem that I was facing is the evaluation of the cells. This can not be done in the same way.

matlab代码类似于

The matlab code is something like

% Parameter initialization
vector1 = ones(1e6);
a=1;
b=2;
c=3;
d=4;

% Formula calculation
vector2(1)=import('input000001.csv');

其中最小输入文件input000001.csv包含一些公式,例如:

where my minimal input file input000001.csv contains some formula, as e.g.:

a*vector1(1),2*vector1(1)/b
c+vector1(1),3*vector1(1)^d

推荐答案

尝试一下

[a b c d] = deal(1,2,3,4);

data = strcat(importdata('input.csv', sprintf('\n')), ';');
Matrix = eval(['[' data{:} '[]]']);

为避免因eval变慢,您可以在另一个m文件中写入字符串,然后在循环中调用它.像这样:

To avoid slowdown by eval you can write the string in another m-file and call it instead in the loop. Something like this:

[a b c d] = deal(1,2,3,4);

matDef = regexprep(fileread('input.csv'), {'(\r\n|\r|\n)' ';^'}, {';' ''});
f = fopen('inputMatrix.m', 'w');
fwrite(f, ['Matrix = [' matDef '];'])
fclose(f);
rehash

for k=1:100000,
    inputMatrix
end

您需要rehash要求matlab将新创建的inputMatrix添加到已知函数列表中.

You need the rehash to ask matlab to add newly-created inputMatrix to list of known functions.

这篇关于评估外部文件字符串中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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