Problem with fscanf.

조회 수: 9 (최근 30일)
Anna Sapegina
Anna Sapegina 2017년 10월 13일
답변: Walter Roberson 2017년 10월 14일
Hello. I haven't written the script, but I used it before. Now it texts out three errors:
"Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in diagram_type (line 459)
optiondata=fscanf(optionfile, '%c'); % read file
Error in PARALYZER (line 281)
minu1,minu1text,maxu2,maxu2text,minu2,minu2text] = diagram_type(project_name);"
There is a code of the script attached... HELP ME, PLEASE !
  댓글 수: 2
OCDER
OCDER 2017년 10월 13일
편집: OCDER 2017년 10월 13일
We need to see diagram_type.m too. The error is saying that your project_name file (optionfile), does not exist, could not be read, or was not opened properly with fopen.
Anna Sapegina
Anna Sapegina 2017년 10월 14일
Hello! Here you are! The file .dat is generated by specilal program, so it's standart. Paralyzer read same files before, so I'm confused by the errors... If it will be useful: D:\PROGRAMS\Paralyzer\ - the path of paralyzer.m, and .dat file D:\PROGRAMS\Paralyzer\Required_Functions\ - the path of diagram_type.m

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

답변 (1개)

Walter Roberson
Walter Roberson 2017년 10월 14일
The code looks in the file content for the phrase "computational option file", and if it finds the phrase, it extracts a file name. If it does not find that phrase, then it uses the name perplex_option.dat instead. Then it attempts to open that file, and it does not check first to be sure it exists, and it does not give a nice message about why it could not open the file.
I recommend improving the code, changing
optionfile=fopen(option_file); % choose & open file
optiondata=fscanf(optionfile, '%c'); % read file
perline_option_file=textscan(optiondata,'%s',...
'delimiter','\n','whitespace',''); % scan per line
perline_option_file=perline_option_file{1}; % take just first dimension
length_options=length(perline_option_file);
for line=1:length_options % loop to read value of auto_refine option (a, m, or o [aut, man, o])
content = num2str(perline_option_file{line});
if strcmp(content(1:12),'auto_refine ')==1
for i=12:length(content) % find position of firstnon-space
if content(1,i)~=' '
auto_refine=content(i); % read character at this position
break
end
end
end
end
fclose(optionfile); % close file
to
auto_refine = 'a'; %default to auto if we do not find option file
[optionfile, msg] = fopen(option_file); % choose & open file
if optionfile < 0
fprintf('skipping unreadable option file "%s", problem was: "%s"', option_file, msg);
else
optiondata=fscanf(optionfile, '%c'); % read file
perline_option_file=textscan(optiondata,'%s',...
'delimiter','\n','whitespace',''); % scan per line
perline_option_file=perline_option_file{1}; % take just first dimension
length_options=length(perline_option_file);
for line=1:length_options % loop to read value of auto_refine option (a, m, or o [aut, man, o])
content = num2str(perline_option_file{line});
if strcmp(content(1:12),'auto_refine ')==1
for i=12:length(content) % find position of firstnon-space
if content(1,i)~=' '
auto_refine=content(i); % read character at this position
break
end
end
end
end
fclose(optionfile); % close file
end

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by