Error with fgetl....why?
14 views (last 30 days)
Matlab keeps giving me a hard time:
"Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in fgetl (line 32)
[tline,lt] = fgets(fid);
Error in plot_test (line 11)
Basically it seems like matlab doesn't like me using fgetl, but why? I use it in the exact same way in another script and it works fine. Basically in this code I'm trying to skip the first 11 lines, then copy the 5th column of data into the Node_CONC file which will compile the output from 1000 monte carlo runs of another program. I attached a Obs_Node.out file for reference. I've tried replacing fgetl with fgets and similarily get an error...
num_sim = 1000; %1000 monte carlo simulations
Node_conc = fopen('\Obs_Node.out','r'); % Open monte carlo output file in Path (i)
skip_lines=11; %skip all the lines until the actually output data
temp = fscanf(Node_conc,'%f',[5,57759]); %scan the matrix of data
TEMP = temp'; % transpose data
temp_Conc = TEMP(:,5); % select the 5th column (concentration)
Node_CONC(:,i) = temp_Conc(:); % save the concentration a new file, one column per iteration of loop
Walter Roberson on 12 Sep 2022
Edited: Walter Roberson on 12 Sep 2022
The fopen is failing. You should get in the habit of checking the return value.
Note that for MacOS and Linux, the \ at the beginning of the file name will be treated just like any other character, and will not be a directory separator. But on Windows a \ at the beginning of the file name means that you are referring to a file that is the top level directory of the current disk drive, such as if you had written C:\Obs_Node.out
We recommend that for portability that you build file names using fullfile() instead of using / or \ in the name.