• Search
  • Register
  • Log in
  • The Math Root » Matlab
  • HOWTO: Saving data using string (variable) filenames in Matlab
  •  
    Anil

    It is an easy matter to save data as a mat(MATLAB binary) file or an ASCII file so long as you know the filename beforehand. For ex: to save data in the variable datvar to a file mydata.dat, all you have to do is say

    
    save mydata.dat datvar
    

    However if you want to dynamically generate your filename, things get a little tricky since MATLAB's documentation is sparse about this. Here's how:

    
    % Create a filename (myfile.001) using sprintf
    fOut = sprintf('myfile.%03d',i);   % say, i=1
    
    % save data as an ASCII file
    save(fOut, 'datvar', '-ascii');
    
    % Or, create a mat file - (myfile001.mat)
    % since MATLAB binary files typically have the extension .mat
    fOut = sprintf('myfile%03d.mat',i);   % say, i=1
    
    % save data as matlab binary
    save(fOut, 'datvar');
    

    If you have multiple columns of data, just ensure that datvar is an array. Remember that the .mat file will retain the name of the variable datvar but the ASCII file will only contain data.

    #8
    Anil
    Member
    Posted 1 year ago
    RSS feed for this topic  

    Reply

    You must log in to post.

      Tags:

  •  © 2009 Anil Kandangath. All posts are owned by the whoever posted them. Share your knowledge.
    The Math Root is proudly powered by bbPress.. This site is not affiliated with The MathWorks™ or Wolfram Research. //   Theme by Mike Lothar  
    [ Time : 0.071s | 8 Queries ]