"Error using fscanf"

조회 수: 6 (최근 30일)
pizzaa
pizzaa 2023년 5월 19일
댓글: pizzaa 2023년 5월 19일
i got error which saying "Invalid file identifier. Use fopen to generate a valid file identifier"
This is the code:
function [z] = Stereo( directory, imagename, numImages)
% ***********************************************************************
% Read the lights and directions:
% ***********************************************************************
lightfile = strcat( directory, '/', 'lights2.txt')
fid = fopen(lightfile, 'r');
numLights = 1;
numLights = fscanf(fid, '%d \n', [1]);
LightMatrix = [];
for i = 1:numLights
lightDir = fscanf(fid, '%f %f %f \n', [3]);
lightDir = lightDir/norm(lightDir);
lightMatrix(i,:) = lightDir;
end
this is the lights2.txt file
Can someone help me i m so curious about this. Bigh thanks for your help
  댓글 수: 1
Stephen23
Stephen23 2023년 5월 19일
편집: Stephen23 2023년 5월 19일
Replace this command
strcat( directory, '/', 'lights2.txt')
with
fullfile( directory, 'lights2.txt')
And forget about using FOPEN & FSCANF: just use READMATRIX to import that file data:
If you use FOPEN then always always always remember to FCLOSE afterwards!

댓글을 달려면 로그인하십시오.

채택된 답변

Askic V
Askic V 2023년 5월 19일
편집: Askic V 2023년 5월 19일
You're getting this error because the path to your file is not correct.
this is where the problem is:
lightfile = strcat( directory, '/', 'lights2.txt')
Print/Display this variable to see the path to the file.
BTW, if your file is structured like in the posted image, I suggest to use readmatrix function instead of fopen and fscanf.
  댓글 수: 3
pizzaa
pizzaa 2023년 5월 19일
can u help me with the code in using readmatrix
pizzaa
pizzaa 2023년 5월 19일
Nice ty so much

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by