Matlab - writing data to .xy files

Click For Summary

Discussion Overview

The discussion revolves around the challenges of saving data to files with varying extensions in MATLAB, particularly when dealing with multiple input files. Participants explore methods to modify file names and extensions while ensuring that the data is saved correctly without overwriting existing files.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a MATLAB code that successfully imports and processes .sp files but struggles with saving them under new extensions without retaining the original extension.
  • Another participant suggests a method for saving matrices with different names and extensions using cell arrays to define file extensions and constructing filenames dynamically.
  • A later reply expresses satisfaction with the proposed solution and indicates an intention to integrate it into their existing code.
  • One participant points out potential errors in the original code regarding the handling of file extensions and the use of the 'save' function, emphasizing the need to separate the filename from its extension using the 'fileparts' function.
  • Another participant confirms they are using an older version of MATLAB and acknowledges the need to manipulate the filename string to remove the original extension before saving it with a new one.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding MATLAB's file handling capabilities, with some agreeing on the need for string manipulation while others highlight potential errors in code syntax. The discussion does not reach a consensus on the best approach, as multiple methods are proposed and debated.

Contextual Notes

Participants mention specific MATLAB versions, which may affect the functionality of certain commands. There is also a recognition that the handling of file extensions and names can lead to errors if not managed correctly, indicating a need for careful coding practices.

Who May Find This Useful

This discussion may be useful for MATLAB users dealing with file input/output operations, particularly those looking to manipulate file names and extensions programmatically.

JeroenS
Messages
5
Reaction score
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
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.
 
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!
 
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?
 
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 separately, 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.
 
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!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
5K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K