Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Data file question

  1. Apr 3, 2009 #1
    How do I make this happen?



    Code (Text):
    E=load('elements.txt');
    EE=2*E;
    save('poop.txt','EE')
     
    It loads elements.txt
    doubles the matrix
    stores the results in a new text file

    this does not work. It creates a new txt file but there is not much in it besides
    the "created on 'this date & time' info"

    I am having trouble deciphering which command I should be using
    for this.

    Any ideas?
    Thanks,
    Casey
     
  2. jcsd
  3. Apr 4, 2009 #2
    Okay, I got it using this:
    Code (Text):
    E=load('elements.txt');
    EE=3*E;
    fid=fopen('poop.txt','w');
    fprintf(fid,'   %8.2f    %8.2f   %8.2f   %8.2f     %8.2f\n ',EE)
    fclose(fid);
    However, this line is what is getting me. I don't know what it means or understand the syntax:
    Code (Text):
    fprintf(fid,'   %8.2f    %8.2f   %8.2f   %8.2f     %8.2f\n ',EE)
     
    the imported file is a 20 x 5 matrix

    then it is multiplied by a scalar and exported to a new file.

    I would like the new file to look like a new 20 x 5 matrix.

    How do I do that?

    Thanks,
    Casey
     
  4. Apr 4, 2009 #3

    MATLABdude

    User Avatar
    Science Advisor

    fprintf is just the standard C style output function (in this case, it's outputting to the file specified by fid instead of to the screen):
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html

    The problem with your approach is that, by default, the 'load' and 'save' commands load and save binary files that encode some matrix. If you use the -ascii option, it'd save more along the lines of what you're trying to do (though you can always use the C-style fprintf and fscanf):
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/load.html
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.html
     
  5. Apr 4, 2009 #4

    f95toli

    User Avatar
    Science Advisor
    Gold Member

    It is usually better to use "dlmread" instead of "load" when handling text files, the former lets you define which delimiter is used (e.g. space or tab) and you can also skip the the first few columns or lines. This is very useful if the file has a header (e.g. the names of the columns).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB Data file question
Loading...