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):
    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?
  2. jcsd
  3. Apr 4, 2009 #2
    Okay, I got it using this:
    Code (Text):
    fprintf(fid,'   %8.2f    %8.2f   %8.2f   %8.2f     %8.2f\n ',EE)
    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?

  4. Apr 4, 2009 #3


    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):

    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):
  5. Apr 4, 2009 #4


    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