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)
x=fgetl(Node_conc);"
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=zeros(57759,num_sim);
for i=1:num_sim
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
for k=1:(skip_lines)
x=fgetl(Node_conc);
end
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
fclose(Node_conc);
end
adsfgad

2 comentarios

Wesser
Wesser el 12 de Sept. de 2022
Also this code works perfectly on my mac and only gives me errors on my microsoft...
Wesser
Wesser el 12 de Sept. de 2022
I have also tried this version and still get an error....
"Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in plot_testpc (line 9)
show=fgets(Node_conc);
num_sim = 1000; %1000 monte carlo simulations
Node_CONC=zeros(57759,num_sim);
for i=1:num_sim
Node_conc = fopen('\Obs_Node.out','r'); % Open monte carlo output file in Path (i)
for k=1:11 %skip all the lines until the actually output data
show=fgets(Node_conc);
end
while ischar(show)
disp(show)
show=fgets(Node_conc);
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
fclose(Node_conc);
end
end

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Sept. de 2022
Editada: Walter Roberson el 12 de Sept. de 2022

0 votos

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.

5 comentarios

Wesser
Wesser el 12 de Sept. de 2022
Ok. That makes sense. Thanks for the guidance!
Wesser
Wesser el 12 de Sept. de 2022
follow up...this is what I modified the code to and I'm still getting the same error. I'm not scripting this correctly..I want the code to loop through different path{i}
"C:\Users\jessi\Desktop\HydrusMC\Simulations\MC_1"
pull data from Obs_Node.out, store it then loop through
"C:\Users\jessi\Desktop\HydrusMC\Simulations\MC_2"
up to
"C:\Users\jessi\Desktop\HydrusMC\Simulations\MC_1000"
doing the same thing. Why isn't this working?
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 Concentration (line 15)
x=fgetl(Node_conc);
num_sim=1000; % Number of Monte Carlo Simulations
Node_CONC=zeros(57759,num_sim); %preallocation for output from all monte carlos
path=cell(1,num_sim);
for i=1:num_sim
work_dir= fullfile('C:\\','Users','jessi','Desktop','HydrusMC','Simulations','MC_','num2str(i)');
mkdir(work_dir);
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
for k=1:(skip_lines)
x=fgetl(Node_conc);
end
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
fclose(Node_conc);
end
%Node_CONC(:,1)=temp(:,2)
Stephen23
Stephen23 el 12 de Sept. de 2022
Replace this literal character vector:
work_dir = fullfile(.. ,'num2str(i)');
with the required function call:
work_dir = fullfile(.. ,num2str(i));
Wesser
Wesser el 12 de Sept. de 2022
I modified the code per your suggestion and it still presents the same error...:{
I want the file path for wtf to read: 'C:\Users\jessi\Desktop\HydrusMC\Simulations\MC_1\Obs_Node.out'
but with the code as scripted below renders:
'C:\Users\jessi\Desktop\HydrusMC\Simulations\MC_\1\Obs_Node.out'
How do I drop the \ between the MC_ and the 1? I couldn't find any advise from other posts on this aspect of fullfile.
num_sim=1000; % Number of Monte Carlo Simulations
Node_CONC=zeros(57759,num_sim); %preallocation for output from all monte carlos
path=cell(1,num_sim);
for i=1:num_sim
wtf= fullfile('C:\\','Users','jessi','Desktop','HydrusMC','Simulations','MC_',num2str(i),'Obs_Node.out');
Node_conc = fopen('wtf','r'); % Open monte carlo output file in Path (i)
skip_lines=11; %skip all the lines until the actually output data
for k=1:(skip_lines)
x=fgetl(Node_conc);
end
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
fclose(Node_conc);
end
%Node_CONC(:,1)=temp(:,2)
Walter Roberson
Walter Roberson el 12 de Sept. de 2022
wtf = fullfile('C:\\', 'Users', 'jessi', 'Desktop', 'HydrusMC', 'Simulations', "MC_"+i, 'Obs_Node.out');
However, this is not portable to other operating systems because of the C:\\ -- and your path is likely to be different on the different operating systems.
You might want to consider something like
wtf = fullfile(userpath, 'HydrusMC', 'Simulations', "MC_"+i, 'Obs_Node.out');
and move the directories to be consistent with that.
Or you might want to consider
if ispc()
desktopdir = fullfile(getenv('USERPROFILE'), 'Desktop');
else
desktopdir = fullfile(getenv('HOME'), 'Desktop');
end
wtf = fullfile(desktopdir, 'HydrusMC', 'Simulations', "MC_"+i, 'Obs_Node.out');

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Productos

Versión

R2022a

Etiquetas

Preguntada:

el 12 de Sept. de 2022

Comentada:

el 12 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by