Changing save name for text file

조회 수: 4 (최근 30일)
Leon Caldeira
Leon Caldeira 2014년 5월 27일
편집: Geoff Hayes 2014년 5월 27일
I am having issues about changing the name of a file using the fprintf/fileID function. When I try to input a string in the fileID, I get the "invalid filname" error.
Here is the function:
S=strcat(cd,'\Results\',answer,'.txt');
fileID=fopen(answer,'w');
fprintf(fileID,'%9s %9s\r\n','Fixation','Duration (ms)');
fprintf(fileID,'%10.5f %10.5f\r\n',Sc);
fprintf(fileID,'\r\n %10s\r\n','Total time');
fprintf(fileID,'%10.5f',Tt);
fclose(fileID);
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2014년 5월 27일
What is answer defined to be? Why define S and then not use it? If I try to run the above with default values for answer, Sc, and Tt then it works fine. Before you try to write to file, the code should check to see if the file identifier is valid:
fileID = fopen(S,'w');
if fileID > 0
% do stuff
% close file
fclose(fileID);
else
fprintf('Error - could not open file %s\n',S);
end

댓글을 달려면 로그인하십시오.

답변 (3개)

Sara
Sara 2014년 5월 27일
Use:
S=strcat(cd,'\Results\',[answer,'.txt']);
  댓글 수: 3
Sara
Sara 2014년 5월 27일
I guess the answer could be
fileID=fopen([answer,'.txt'],'w');
given how you were creating S.
Leon Caldeira
Leon Caldeira 2014년 5월 27일
편집: Leon Caldeira 2014년 5월 27일
It didn't work again. Here's the error log:
??? Error using ==> fopen
Invalid filename.
Error in ==> EyeTracker at 226
fileID=fopen([answer,'.txt'],'w');
This is how I get the 'answer' string:
answer =
'Leon'

댓글을 달려면 로그인하십시오.


Roberto
Roberto 2014년 5월 27일
possible causes:
  • answer contains invalid charater(s) for a file name
  • more or less about the same error but the invalid character comes from CD (most comun error: current folder might contain spaces e.g. c:\my folder\ ).
Possible solutions:
  • change the current directory to one with no-spaces in the path like C:\myfolder\
  • read some solutions here
  댓글 수: 1
Leon Caldeira
Leon Caldeira 2014년 5월 27일
Roberto, thank you for the answer.
About the directory path, when I use the default option (inputing a text to the filename) there's no problem. But when I input a variable string, it comes with the error. Also, I tried with no invalid characters and it did not work.

댓글을 달려면 로그인하십시오.


Geoff Hayes
Geoff Hayes 2014년 5월 27일
편집: Geoff Hayes 2014년 5월 27일
Given your comment as to the return value for answer in Sara's response to your question, I almost think that answer is a cell array rather than a string. Just prior to evaluating
fileID=fopen([answer,'.txt'],'w');
type in the command window
[answer,'.txt']
and
class(answer)
What is returned? If I type the following answer initializations in my command window I see
>> answer = 'Leon'
answer =
Leon
>> answer = {'Leon'}
answer =
'Leon'
The second matches your return value. If it is indeed a cell array, then either replace with the string equivalent OR access the string value via answer{1} as a string must be passed as an input to the fopen function.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by