Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 168831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:31:21+00:00 2026-05-11T12:31:21+00:00

I would like to load variables from a text file. For example, my text

  • 0

I would like to load variables from a text file.

For example, my text file, varA, varB, and varC.

In MATLAB, I would like to give these variables values so that every variable is a 2×2 matrix.

So from the text file containing the above information I would get a matrix that looks like this:

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

Is this possible?

I added a second example to try to make things a little clearer.

My text file, text.txt, looks like this

x1 x2 x3 

In MATLAB my .m file gives the 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 (GA) on a very huge problem (of travelling salesman problem (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, let’s 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…

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:31:22+00:00Added an answer on May 11, 2026 at 12:31 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 90k
  • Answers 90k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have you run through the trouble shooting in the documentation? May 11, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer You want the append flag, ios::app, if you want to… May 11, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer You can swap the vector with an empty one -… May 11, 2026 at 6:04 pm

Related Questions

I'd like to store the contents of a data structure, a few arrays, and
Scenario: I load some data into a local MySQL database each day, about 2
I'm a total python noob so please bear with me. I want to have
I need to develop a system for storing large numbers (10's to 100's of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.