From: ismaelf on
Hi!

Inside a function I do the fopen and textscan actions twice. I do not have any problems in the first time, but in the second, the values are not assigned to the matrix 'B'. The 'load(file.txt')' does not work either.

Thank you in advance.

I show you the code:

fid=fopen(archivo_final,'r+');
A=textscan(fid,'%n %n %n %*n %n %*n %*n %*n %*s %*n %*n %*n','HeaderLines',6);
A=horzcat(A{1,1},A{1,2},A{1,3},A{1,4});
fclose(fid);

fid2=fopen(archivo_inicial,'r+');
B=textscan(fid2,'%n %n %n %n %n');
B=B{1,1};
fclose(fid2);
From: Walter Roberson on
ismaelf(a)ual.es FERNANDEZ wrote:

> Inside a function I do the fopen and textscan actions twice. I do not
> have any problems in the first time, but in the second, the values are
> not assigned to the matrix 'B'. The 'load(file.txt')' does not work either.

> fid=fopen(archivo_final,'r+');
> A=textscan(fid,'%n %n %n %*n %n %*n %*n %*n %*s %*n %*n
> %*n','HeaderLines',6);
> A=horzcat(A{1,1},A{1,2},A{1,3},A{1,4});
> fclose(fid);
>
> fid2=fopen(archivo_inicial,'r+');
> B=textscan(fid2,'%n %n %n %n %n');
> B=B{1,1};
> fclose(fid2);


I suggest you use the two-output form of fopen:

[fid2, status2] = fopen(archivo_inicial,'r+');
if fid2 < 0
disp('Open inicial failed because: ', status);
else
B=textscan(fid2,'%n %n %n %n %n');
B=B{1,1};
fclose(fid2);
end