当前位置:  开发笔记 > 编程语言 > 正文

在MATLAB中加载文本文件中的值

如何解决《在MATLAB中加载文本文件中的值》经验,为你挑选了1个好方法。

我想从文本文件加载变量.

例如,我的文本文件,varA,varB,和varC.

在MATLAB中,我想给出这些变量值,以便每个变量都是2x2矩阵.

所以从包含上述信息的文本文件中我得到一个如下所示的矩阵:

[ 1 2 3 4 5 6;
  1 2 3 4 5 6]

这可能吗?

我添加了第二个例子,试图让事情变得更加清晰.

我的文本文件text.txt看起来像这样

x1 x2 x3

在MATLAB中,我的.m文件给出了这些变量的值

x1 = [1 1; 1 1]
x2 = [2 2; 2 2]
x3 = [3 3; 3 3]

所以,当我导入我的文本文件时,我会得到

a = (textfile)
a = [1 1 2 2 3 3 ; 1 1 2 2 3 3]

我基本上尝试使用遗传算法(GA)来解决一个非常大的问题(旅行商问题(TSP)类型).问题是我拥有的每个变量都是一个矩阵,交叉,适应度和变异代码变得相当复杂.我也有制作随机开始人口的问题.

我想从256列表中随机选择,比方说30个变量,这样变量只能被选中一次.但是,每个变量在2*2矩阵中都有自己的特定值,无法更改.

我想使用randperm,然后x在每个值之前放置一个变量而不是值...



1> gnovice..:

如果文本文件中的数据看起来像这样(字符串用空格分隔):

x1 x2 x3 ...

您可以使用TEXTSCAN将字符串读入单元格数组,如下所示:

fid = fopen('file.txt','r');
A = textscan(fid,'%s');
fclose(fid);
A = A{:};

A现在将字符串存储在单元格数组中:{'x1'; 'X2'; "×3"; ......}.现在,要从其中一个字符串中创建一个变量并为其赋值,我会使用ASSIGNIN:

assignin('base',A{1},[1 2; 1 2]);

This will create a variable x1 in the base workspace and assign it the value [1 2; 1 2]. The first argument can be either 'base' or 'caller' to create a variable in either the MATLAB base workspace or the workspace of the caller function. You would repeat this for each string name in A, giving it whatever value you want.


ALTERNATE OPTION:

This is an alternate answer to the one I gave you above. The above answer addresses the specific problem you raised in your question. This answer gives you a whole other option to potentially avoid doing things the way you were describing them in your question, and it will hopefully make things easier for you...

If I understand your problem, you basically have 256 2-by-2 matrices, and you want to randomly pick 30 of them. Each of these 2-by-2 matrices sounds like it is stored in its own variable (x1 to x256). Instead, I would suggest storing all 256 matrices in just one variable as either a 3-D array:

xArray = zeros(2,2,256);     % Initialize all matrices as [0 0; 0 0]
xArray(:,:,1) = [1 1; 2 2];  % This enters a value for the first matrix

or a cell array:

xArray = cell(1,256);    % Initializes an empty array of cells
xArray{1} = [1 1; 2 2];  % Enters a value for the first matrix

You would have to initialize all the values first. Then if you want to randomly pick 30 values, you can next randomize the order of either the third dimension of the 3-D array or the order of the cell array by using RANDPERM:

startOrder = 1:256;              % The default order of the matrices
index = randperm(256);           % Randomly order the numbers 1 to 256
xArray = xArray(:,:,index);      % For a 3-d array
xArray = xArray(index);          % For a cell array

Then just use the first 30 entries in xArray for your calculations (instead of the individual variables like you were before):

x = xArray(:,:,1);  % Puts the first matrix from the 3-D array in x
x = xArray{1};      % Puts the first matrix from the cell array in x

You can keep repeating the use of RANDPERM to keep generating new randomized arrays of matrices. If you have to keep track of which original matrices you are using, you have to add this line after you randomize xArray:

startOrder = startOrder(index);

Now the entries of startOrder will tell you the original position a matrix was in. For example, if the first array entry in startOrder is 40, then the matrix in the first position of xArray was originally the 40th matrix you entered when you initialized xArray.

Hope this helps!

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有