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')

if g == 1
fileID = fopen('output2\cycle_graph_output.txt','w')%Creates a output text file in folder output2
fprintf(fileID,'edges\t initial_nodes\t nodes_after_reduction\t difference \n')
myfolderinfo = dir('cycle_graph') ; %lists folder contents
N = length(myfolderinfo) ;
so the code is working perfectly when i'm using the Matlab in my local system.
But its generating the error when i tried to run it on Matlab online. What changes can i make to make it work on Matlab online

Antworten (1)

MATLAB Online does not use MS Windows; it uses Linux. The directory separator is / not \
MS Windows is happy to use / instead of \ so you should just change the \ to /
Better yet, use fullfile()
... And of course make sure you have a directory named output2 relative to where you are running the code.

6 Kommentare

f = fullfile('output2','cycle_graph_output.txt')
fileID = fopen(f,'w')
It's still showing the same error
What shows up for
dinfo = dir('.');
dinfo(~[dinfo.isdir]) = [];
{dinfo.name}
!ls -ld output2
It is used to access various files in a folder
fileID = fopen('cycle_graph_output4.txt','w')%Creates a output text file
so i just removed the folder.so the above statement craetes a output text file.Its working fine in my local system.But when i try the same thing is Matlab online it is not creating a file,rather the it is displaying output as
file id= some value
Call fopen with two outputs and show us both the output values that get displayed when you run that code in MATLAB Online.
fileID = fopen('cycle_graph_output4.txt','w')%Creates a output text file
would be expected to display a number as well as creating the output file. If the number is negative then file creation failed; if the number is positive then file creation worked.
On MATLAB online, what shows up if you execute this command:
!pwd; ls -ld .; whoami
Note that the ! at the beginning is part of the command.

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 16 Mär. 2020

Kommentiert:

am 19 Mär. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by