even after using fopen, still giving negative fid. how to get a valid fid?

조회 수: 18 (최근 30일)
shashwat soni
shashwat soni 2018년 10월 19일
댓글: Rik 2020년 8월 19일
clc;
clear;
close all;
filename = fullfile(matlabroot,'1.txt');
fid = fopen('1.txt','r') ;
C = fscanf(fid,'%c');

답변 (2개)

Image Analyst
Image Analyst 2018년 10월 19일
Try actually passing the filename to fopen():
clc
clear;
close all;
filename = fullfile(matlabroot,'1.txt');
if exist(filename, 'file')
fid = fopen(filename ,'rt') ;
C = fscanf(fid,'%c');
else
message = sprintf('File does not exist', filename);
uiwait(errordlg(message));
return;
end

Stephen23
Stephen23 2018년 10월 19일
You need to actually pass the filename to fopen:
fnm = fullfile(matlabroot,'1.txt');
[fid,msg] = fopen(fnm,'r');
assert(fid>=3,msg)
C = fscanf(fid,'%c');
fclose(fid);
  댓글 수: 2
elnaz Alikarami
elnaz Alikarami 2020년 8월 19일
I have the same problem and non of these helped. the fid keeps being -1 and I don't know how to fix it. can someone help me please?
Rik
Rik 2020년 8월 19일
@elnaz Alikarami: you will have to post your code, ideally in your own question. Feel free to post a link here to make people aware of your question. Also have a read here and here. It will greatly improve your chances of getting an answer.

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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by