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 个评论
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
2017-7-18
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.
回答(1 个)
Jan
2017-7-18
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
类别
在 帮助中心 和 File Exchange 中查找有关 App Building 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!