views:

3157

answers:

1

I would like to load variabless from an textfile.

e.g my text file varA varB varC

In Matlab I would like to give these variables values so that every var is a 2x2 matrix

so from the textfile containing the above information i would get an matrix that looks like this: [ 1 2 3 4 5 6; 1 2 3 4 5 6]

Is this possible?

added a second example to try to make things a little clearer. my textfile text.txt looks like this x1 x2 x3 In matlab my m file gives values to these variables like x1=[1 1; 1 1] x2=[2 2; 2 2] x3=[3 3; 3 3]

so When I import my textfile I would get a=(textfile) a= [1 1 2 2 3 3 ; 1 1 2 2 3 3]

I basically try to adapt a Genetic Algorithm on a very huge problem (of TSP type). The problem is that every variable i have is a matrix and the crossover, fitness and mutation codes get pretty complicated. And I am having problems of making a random start population as well.

I would like to randomly select lets say 30 variables from a list with 256 so that the variable can only be picked once. Each variable however have their own specific values in a 2*2 matrix that cannot be changed. I would like to use randperm and then put an x before every value making them variables instead of values...

Thanx a million for all your help I really appriciate it!

+5  A: 

If the data in the text file looks like this (strings separated by spaces):

x1 x2 x3 ...

You can read the strings into a cell array using TEXTSCAN like so:

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

A now stores the strings in a cell array: {'x1'; 'x2'; 'x3'; ...}. Now, to make a variable out of one of these strings and assign it a value, I would use 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!

gnovice
thanx for the fast answer...however I want to do it the other way around. Meaning that My text file contains names of variables like VarA ,VarB...Then I could load them to Matlab where every var would become a 2x2 matrix..e.gtext = x in matlabx=[0 1; 2 3]
I've updated my answer to address the revised question.
gnovice
thx. However I would like to Import the variables without having to name them again(256 variables that will change in order in a loop) so basically my only problem is to get the '' away from the text arrays while using textscan. so that x would be x and not 'x'
I don't think I follow you when you say "x would be x and not 'x'". If x is a string in your text file, a variable called x (not 'x') will be made in your workspace using the above code. Apostrophes in MATLAB are used to define a string value, which TEXTSCAN returns but ASSIGNIN makes into a var.
gnovice
ok... Thanks for all your help... getting close..Im mean my text file will change with every loop and may be x2 x1 instead of x1 x2 the next time... so The value of the variables would have to be defined before I read them in from the text...
Thank you very much...That's exactly what I needed...