Changing output file name for multiple files

if true
%%Read in Data
[FileName, PathName] = uigetfile(...
{'*.ras;*.raw;*.asc',...
'Rigaku files (*.ras, *.raw, *.asc)';
'*.*', 'All files'},...
'Pick a file');
[FileName_out, PathName_out] = uiputfile('*.ras');
readFileID = fopen([PathName FileName], 'r');
writeFileID = '';
fileNum = 1;
line = fgetl(readFileID); % Get the first line in the file
while(line ~= -1) % -1 Indicates end of file
if(strcmp(line,'*RAS_HEADER_START'))
%Open new file to write data to
*writeFileID = fopen(['data-file-', num2str(fileNum), '.ras'], 'w');*
fileNum = fileNum + 1;
elseif(strcmp(line,'*RAS_INT_END'))
fclose(writeFileID); % Close Current File
writeFileID = '';
elseif(~ strcmp(writeFileID,''))
%Write line to current file
fprintf(writeFileID,'%s\n',line);
end
line = fgetl(readFileID); % Get the next line in the file
end
end
I currently have a program that takes in data and then parses it into individual files. I want to turn this into an executable file so that others can use it. Unfortunately I have it as data-file- so it saves as 1,2,3... but if someone runs the program again it will write the new file onto the old one. How can I use uiputfile or something so that it is possible for someone to put in a name for each new individual file? I uploaded a sample file (in txt form)

3 comentarios

Jan
Jan el 18 de Jul. de 2017
Editada: Jan el 18 de Jul. de 2017
I do not understand the question. Do you want to check, if the created file name is existing already, and if so proceed to the next free name?
Your readFileID is not closed. (~ strcmp(writeFileID,'')) is a strange way to check the validity of the FileID. Better use the default -1.
This is not twitter: No # before the tags.
Sharon Morgan
Sharon Morgan el 18 de Jul. de 2017
I want it to be possible for someone to input a file name for each new file thru a dialog box.
Jan
Jan el 18 de Jul. de 2017
Editada: Jan el 18 de Jul. de 2017
Then do not use
['data-file-', num2str(fileNum), '.ras']
but uputfile to obtain the path and file name. You did this already in
[FileName_out, PathName_out] = uiputfile('*.ras');
but do not use FileName_out and PathName_out anywhere. Therefore I do not understand the purpose of the code.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 18 de Jul. de 2017
if(strcmp(line,'*RAS_HEADER_START'))
%Open new file to write data to
Default = fullfile(PathName, sprintf('data-file-%d.ras', filenum);
[FileName, PathName] = uiputfile('*.ras', 'Output file', Default);
if ~ischar(FileName)
disp('User has aborted the file choosing');
return
end
fileNum = fileNum + 1;
writeFileID = fopen(fullfile(PathName, FileName), 'w');
if writeFileID == -1
error('Cannot open file for writing: %s', fullfile(PathName, FileName));
end

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 18 de Jul. de 2017

Respondida:

Jan
el 18 de Jul. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by