Results 1 to 4 of 4

Math Help - Loading Data in Matlab

  1. #1
    Newbie
    Joined
    May 2009
    Posts
    2

    Loading Data in Matlab

    Hi Guys,
    I'm trying to read some data into Matlab. This is what I've done so far.
    >> selwoodData = zeros(53);
    >> load selwoodData.txt;
    Now what I would like to do is pick off each column into a column vector variable. Is there a way to do this without hard coding it. I was thinking of setting up a loop that read each of the 53 columns into different variables, but I'm not sure how I would change the variable name each time. Any help would be appreciated.
    Thanks
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by notPrime View Post
    Hi Guys,
    I'm trying to read some data into Matlab. This is what I've done so far.
    >> selwoodData = zeros(53);
    >> load selwoodData.txt;
    Now what I would like to do is pick off each column into a column vector variable. Is there a way to do this without hard coding it. I was thinking of setting up a loop that read each of the 53 columns into different variables, but I'm not sure how I would change the variable name each time. Any help would be appreciated.
    Thanks
    I'm not at a machine that I can run Matlab on at present so can't check the syntax, but something like the following should do the trick:

    Code:
    sz=size(selwoodData)
     
    for idx=1:sz(2)
      eval(['rootname',num2str(idx),'=selwoodData(:,idx)']);
    end
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    May 2009
    Posts
    2
    Thanks a lot. I have another question (Sorry, I literally started using Matlab last week). I have two m files fitness and DoIt. In DoIt, I have:
    ...Some code
    fitness(gene2,gene3,gene4,gene5,gene6,actualYValue s,measureOfFit1)
    In fitness, I have:
    %This function measures the fitness of our chromosome by comparing the predict values to the
    %actual values. The measure of fit is the square of the pearson correlation coefficient.

    function[measure] = fitness(x1, x2, x3, x4, x5,yValues,fitnessValue)
    %Here we form the matrix and carry out the linear regression calculations
    firstColumn=ones(29,1);
    theMatrix = [firstColumn, x1, x2, x3, x4, x5];
    c=inv(transpose(theMatrix)*theMatrix)*transpose(th eMatrix);
    %Now we will multiply the transpose of the c matrix by the the transpose of theMatrix to
    %calculate our predicted y values. Using linear regression.
    finalMatrix = transpose(transpose(c)*transpose(theMatrix));
    predictedYValues = finalMatrix*yValues;
    measure = linreg(predictedYValues, yValues);
    fitnessValue=measure;

    I'm trying to get the fitness function to reflect the changes in the fitnessValue done in the fitness function back to the DoIt function. How would I do this? Is there anything like the ampersand in C?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by notPrime View Post
    Thanks a lot. I have another question (Sorry, I literally started using Matlab last week). I have two m files fitness and DoIt. In DoIt, I have:
    ...Some code
    fitness(gene2,gene3,gene4,gene5,gene6,actualYValue s,measureOfFit1)
    In fitness, I have:
    %This function measures the fitness of our chromosome by comparing the predict values to the
    %actual values. The measure of fit is the square of the pearson correlation coefficient.

    function[measure] = fitness(x1, x2, x3, x4, x5,yValues,fitnessValue)
    %Here we form the matrix and carry out the linear regression calculations
    firstColumn=ones(29,1);
    theMatrix = [firstColumn, x1, x2, x3, x4, x5];
    c=inv(transpose(theMatrix)*theMatrix)*transpose(th eMatrix);
    %Now we will multiply the transpose of the c matrix by the the transpose of theMatrix to
    %calculate our predicted y values. Using linear regression.
    finalMatrix = transpose(transpose(c)*transpose(theMatrix));
    predictedYValues = finalMatrix*yValues;
    measure = linreg(predictedYValues, yValues);
    fitnessValue=measure;

    I'm trying to get the fitness function to reflect the changes in the fitnessValue done in the fitness function back to the DoIt function. How would I do this? Is there anything like the ampersand in C?
    Try calling code:

    Code:
    fitnessvalue=fitness(  ..... )
    where the fitness function returns measure rather than modifies fitnessvalue:

    Code:
    function [measure] = fitness(x1, x2, x3, x4, x5,yValues,fitnessValue)
    %Here we form the matrix and carry out the linear regression calculations
     
      firstColumn=ones(29,1);
      theMatrix = [firstColumn, x1, x2, x3, x4, x5];
      c=inv(transpose(theMatrix)*theMatrix)*transpose(theMatrix);
      %Now we will multiply the transpose of the c matrix by the the 
      %transpose of theMatrix to 
      %calculate our predicted y values. Using linear regression.
      finalMatrix = transpose(transpose(c)*transpose(theMatrix));
      predictedYValues = finalMatrix*yValues;
      measure = linreg(predictedYValues, yValues);
    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. MATLAB: Loading a file and digits
    Posted in the Math Software Forum
    Replies: 2
    Last Post: May 20th 2010, 09:55 AM
  2. extracting data from matlab data structures
    Posted in the Math Software Forum
    Replies: 4
    Last Post: May 19th 2010, 05:12 PM
  3. importing data into octave/matlab
    Posted in the Math Software Forum
    Replies: 0
    Last Post: November 5th 2008, 10:20 AM
  4. Saving data (Matlab Question)
    Posted in the Math Software Forum
    Replies: 2
    Last Post: June 23rd 2008, 09:00 AM
  5. Matlab: Loading from strings
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 10th 2008, 12:13 PM

Search Tags


/mathhelpforum @mathhelpforum