My error is the ever popular ::::Error using fprintf Invalid file identifier. Use fopen to generate a valid file identifier. Error in pro1 (line 7) fprintf(fileID,'edges\t initial_nodes\t nodes_after_reduction\t difference \n')

2 views (last 30 days)
prompt="enter g value";
g=input(prompt);%To choose the type of graph
if g == 1
fileID = fopen('output2\cycle_graph_output.txt','w')%Creates a output text file
fprintf(fileID,'edges\t initial_nodes\t nodes_after_reduction\t difference \n')
myfolderinfo = dir('cycle_graph') ; %lists folder contents
N = length(myfolderinfo) ; % number of files in the folder
else if g == 2
fileID = fopen('output2\wheel_graph_output.txt','w')
fprintf(fileID,'edges\t initial_nodes\t nodes_after_reduction\t difference \n')
myfolderinfo = dir('wheel_graph') ;
N = length(myfolderinfo) ;
else if g == 3
fileID = fopen('output2\windmill_graph_output.txt','w')
fprintf(fileID,'edges\t initial_nodes\t nodes_after_reduction\t difference \n')
myfolderinfo = dir('windmill_graph') ;
N = length(myfolderinfo) ;
end
end
end
for i = 3:N %To iterate through every file in the respective directory
thisfile = myfolderinfo(i).name
A=readmatrix([myfolderinfo(i).folder '\' myfolderinfo(i).name],'Whitespace',' []');%Reads data as matrix from all the files in the respective directory
[n,k] = size(A);
///this code works fine in the Matlab in my local system.But the errors arise when i try to run it on Matlab online.What changes can i make to it to work on Matlab online?

Answers (1)

Steven Lord
Steven Lord on 17 Mar 2020
You never check that fopen was able to successfully open the file for writing before you try writing to it. Call fopen with two output arguments. If the first output argument is -1, check the second output argument for more information about why fopen was unable to open the file.

Communities

More Answers in the  Distance Learning Community

Categories

Find more on Platform and License 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!