Matlab - Ayuda con una función

 
Vista:

Ayuda con una función

Publicado por saber (3 intervenciones) el 09/02/2023 19:12:04
Me gustaria añadir en esta función que si no encuentra el fichero o no puede abrirlo, la función debe devolver un vector vacío para overlap y NaN para scaling.

function [overlap, scaling]= get_parameters(ruta)
if nargin==0
error('Falta de argumentos');
else

fid = fopen(ruta);
linea = fgetl(fid);
contador=1;
while(ischar(linea))
if contador==1
overlap=fscanf(fid, '%f', [1 1024]);
end
if strfind(linea, 'scaling')
scaling = sscanf(linea, 'scaling: %f');


fprintf('Scaling de %f', scaling );
end
linea = fgetl(fid);
contador = contador + 1;
end
fclose(fid);

end
end



Si alguien me pudiera ayudar estaría muy agradecido.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Richtofen

Ayuda con una función

Publicado por Richtofen (20 intervenciones) el 10/02/2023 10:00:06
Solo tienes que comprobar que fid no sea -1, en cualquier otro caso el fichero se he leido correctamente

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function [overlap, scaling]= get_parameters(ruta)
    if nargin==0
        error('Falta de argumentos');
    else
        fid = fopen(ruta);
        if fid == -1
            disp('Archivo no encontrado');
            overlap = [];
            scaling = NaN;
        else
            linea = fgetl(fid);
            contador=1;
            while(ischar(linea))
                if contador==1
                    overlap=fscanf(fid, '%f', [1 1024]);
                end
                if contains(linea, 'scaling')
                    scaling = sscanf(linea, 'scaling: %f');
                    fprintf('Scaling de %f', scaling );
                end
                linea = fgetl(fid);
                contador = contador + 1;
            end
            fclose(fid);
        end
    end
end
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar