Problem with fscanf.

8 views (last 30 days)
Anna Sapegina
Anna Sapegina on 13 Oct 2017
Answered: Walter Roberson on 14 Oct 2017
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 Comments
OCDER
OCDER on 13 Oct 2017
Edited: OCDER on 13 Oct 2017
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 on 14 Oct 2017
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

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 14 Oct 2017
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

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!