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

Matlab - writing data to .xy files

  1. Oct 1, 2009 #1
    for a project i have to open, edit and save a varying amount of files. I use a matlab code for this. It works fine and imports all the .sp files in the directory with the fopen() command. The only problem i still have is that i also want to change the extension of the files. Right now i use dlmwrite inside a loop that that saves each and every file:

    dlmwrite(files(j).name,x,'newline', 'pc','delimiter','\t')

    In that case it perfectly saves all the editted files with the same name but also the same extension! I tried to use the 'save' command and that works but in that case its the other way around and the extension is changed but the filename is fixed so that it overwrites. Is there a way to save a matrix to a file with varying filename AND extension inside a loop?

    Here is the crucial part of the code i have:

    files = dir('Input\*.sp');
    total=length(files);

    for j=1:length(files)

    cd('Input')

    fid = fopen(files(j).name, 'r');
    grades = textscan(fid, '%f %f %f', n, 'headerlines', h);
    fclose(fid);

    x=[grades{1,1} grades{1,2}];

    x=sortrows(x,c);

    for z=1:length(x)
    x(z,2)=10^-x(z,2);
    end

    cd ..
    cd('Converted files')

    dlmwrite(files(j).name,x,'newline', 'pc','delimiter','\t') %write file

    cd ..

    end
     
  2. jcsd
  3. Oct 2, 2009 #2
    This is my solution for saving matrix into different names and extension (im not sure that i understand your question correctly):

    Code (Text):

    p = rand(1, 10);
    q = ones(10);

    cEXT{1, 1} = '.txt';           // indicates file extension in cell
    cEXT{1, 2} = '.xls';
    cEXT{1, 3} = '.dat';

    for j = 1 : 3
        filename{1, j} = strcat('name', num2str(j), cEXT{1, j});        //indicates file name
    end

    for j = 1 : 3
        save(filename{1, j}, 'p', 'q', '-ASCII');
    end
     

    Hope this help.
     
  4. Oct 3, 2009 #3
    It was indeed a bit difficult to describe my problem but this is what i was looking for. Im pretty sure that i can fit this in my code. Thanks!
     
  5. Oct 4, 2009 #4
    I've been able to implement it and it works! The only thing now is that i load all the files with:

    files = dir('Input\*.sp');

    and then recall the names of the files with: files(j).name. In this case the names of the files include the extension .sp. so when i later save the file like:

    cEXT(1,1)= '.xy';

    for j=1:length(files)
    save(files(j).name, cEXT(1,1));
    end

    than i get files that look like: name.sp.xy... where i want it to be just name.xy. Is there a simple solution to this?
     
  6. Oct 4, 2009 #5
    Before discussing about your problem, I want to make something more clearer.

    1) First, about your code:

    Code (Text):
    cEXT(1,1)= '.xy';

    for j=1:length(files)
    save(files(j).name, cEXT(1,1));
    end
    idk which version of matlab you are using but in my matlab (7.8), these codes will give me an error. When you set a string (in this case '.xy') to a member of one array, this string is not considered a member of array but characters inside the string will be treated as a member. It means that with the string '.xy', you will have a 3-member-array. Therefore, 1st line will throw an error. To avoid this, i used curly brackets ( { } ) to creat a cell-array not parantheses ( ( ) ).

    2) Secondly, about 'dir' function and 'file.name'. After using dir, command file.name will return a fullname of the file. For example, you have a file named abcdef.xyz, command file.name will return a result: abcdef.xyz not abcdef. If you want achieve file name and file extension seperately, you should use 'fileparts' function.

    3) Next thing is the syntax of 'save' function in matlab. In my matlab, the syntax is:

    Code (Text):
    save('filename', 'var1', 'var2', ...)
    In this syntax, variable 'filename' already has extension of the file (in default). Variables 'var1', 'var2' is the name of variables declared previously (for example: from my post above, i had 2 variables: p, q). And in the case of your code, you have filename = files(j).name and cEXT(1, 1) = var1 ==> error. :(

    ==> idk how you achieve name.sp.xy with your codes :(
    -----

    According to my knowledge about matlab, matlab does not provide a function for renaming a file. Therefore, in order to change extension of file, you should creat new file and then open, copy contents of old file to a new file. I hope there is a better way to do it :(

    Hope this help.
     
  7. Oct 5, 2009 #6
    I have a student version of matlab called r2009

    I did use curly brackets instead of parentheses so that is a mistake in my code written here. Furthermore, files.name indeed gives the full name so when i use the save command and add the extension .xy with cEXT{1,1} then it returns name.sp.xy. And you're right that if you want to store a matrix in the file you will have to change the syntax but with the code i wrote here i was just checking for the filenames.

    Anyway i have been able to come to a solution. I manipulated the files.name string to ' cut off' the .sp before saving it with a new extension. Probably it can be done much easier but it works fine now!

    Thanks very much for the help!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab - writing data to .xy files
Loading...