Matlab - writing data to .xy files

In summary: Hope this helps.In summary, the code saves all the files with the same name and extension with different member inside a loop.
  • #1
JeroenS
5
0
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
 
Physics news on Phys.org
  • #2
This is my solution for saving matrix into different names and extension (im not sure that i understand your question correctly):

Code:
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.
 
  • #3
It was indeed a bit difficult to describe my problem but this is what i was looking for. I am pretty sure that i can fit this in my code. Thanks!
 
  • #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?
 
  • #5
Before discussing about your problem, I want to make something more clearer.

1) First, about your code:

Code:
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:
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.
 
  • #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!
 

FAQ: Matlab - writing data to .xy files

What is a .xy file in Matlab?

A .xy file in Matlab is a text file that contains data points in the form of x and y coordinates. It is commonly used for storing and sharing data in scientific applications.

How do I write data to a .xy file in Matlab?

To write data to a .xy file in Matlab, you can use the "fprintf" function. This function allows you to specify the format of the data and the file name to be written to. You can also use the "dlmwrite" function to write data to a .xy file, which automatically formats the data in a tab-delimited format.

Can I append data to an existing .xy file in Matlab?

Yes, you can append data to an existing .xy file in Matlab using the "fprintf" function with the "a" flag. This will append the new data to the end of the file without overwriting any existing data.

How do I read data from a .xy file in Matlab?

To read data from a .xy file in Matlab, you can use the "importdata" function. This function will automatically detect the format of the file and import the data into a matrix or structure. You can also use the "fscanf" function to read data from a .xy file, which allows for more control over the data format.

Can I convert a .xy file to a different format in Matlab?

Yes, you can convert a .xy file to a different format in Matlab using the "saveas" function. This function allows you to specify the new file format, such as .csv or .txt, and save the data in the desired format. You can also use the "dlmwrite" function with a different file extension to convert the file to a different format.

Similar threads

Replies
8
Views
847
Replies
1
Views
3K
Replies
4
Views
7K
Replies
1
Views
3K
Back
Top