How to specifiy the data format to export generated data from matlab?!

Hi Guys,
I try to save data to a text format. The name of the data exported is:
name=[Model_File '__ Timeslot_STI: ' num2str(Timeslot)]
fid = fopen(name,'w'); % Create/open file discarding content
now i dont know how to specify the file type, when i creat this document. I would like to save it as txt.file... Does anyone know how to arrange this?
I am very glad for your help! Best regards, John

 Accepted Answer

name = sprintf('%s__ Timeslot_STI: %g.txt', Model_File, Timeslot);
fid = fopen(name, 'wt'); % Create/open file discarding content

5 Comments

Hi Walter, thx for your answer...its strange but when i execute this code:
x = 0:.1:1;
A = [x; exp(x)];
name = sprintf('%s__ Timeslot_STI: %g.txt', Model_File, Timeslot)
fullFileName = fullfile('C:\Users\powersyslab\Desktop', name)
fileID = fopen(fullFileName,'w');
fprintf(fileID,'%6s %12s\r\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\r\n',A);
fclose(fileID);
i dont get a txt.file. the file created just has specific type. When i try to open it, i have to chose a programm for it, like notepad e.g...it is not automatically associated as txt. file
Best regards, John
You are using MS Windows: I recommend you create your text files with 'wt' rather than 't'. We have an active question at the moment from someone who is being fouled up by the lack of the \r .
But that does not explain why MS Windows would not associate the file with Notepad or the like. I do not know how (or why) MS Windows makes it decisions :(
By the way, since you said Notepad: Notepad is one of the programs that needs the \r that using 'wt' would give but 'w' would not.
thx for your reply walter:) now i see there is another problem occuring with the name of the file as well as with the content.
When i execute:
x = 0:.1:1;
A = [x; exp(x)];
name = sprintf('%s__ Timeslot_STI: %g.txt', Model_File, Timeslot)
fullFileName = fullfile('C:\Users\powersyslab\Desktop', name)
fileID = fopen(fullFileName,'wt');
fprintf(fileID,'%6s %12s\r\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\r\n',A);
fclose(fileID);
the file is created with the correct name but no content is inside!!
if i use the line
fileID = fopen('fullFileName','wt');
instead i get as name of course fullFileName but now it is filled with content!!? i am quite confused ...sry for asking this strange things here but i am new to exporting data and right now i am not aware of how it works.
Best regards, John
Hi Walter,
ext='.txt'; %extension in which files will be saved
filename=['text_dyn',ext];
% Create/open file discarding content
fid = fopen(filename,'wt');
works fine with me and creates a textfile:)
Colons (':') are not permitted in MS Windows file names as they are reserved for drive specifications.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!