Could anyone explain the code for me ?
이전 댓글 표시
file_path = dir(''); fid = fopen(''); pattern_col = 0;
while ~feof(fid)
pattern_col = pattern_col + 1; %coloum increment is 1
exp = []; %initialize the sample data
area_sample = [];
angle_sample = [];
fcontent = fgetl(fid); %get pattern size
pattern_size = fcontent;
fcontent = fgetl(fid); %get time interval
pattern_hour = fcontent;
fcontent = fgetl(fid); %get file path
file_path_disp = fcontent;
file_path = dir(fcontent); %should be specific
file_num = size(file_path, 1);
Could anyone explain the code for me ? It can read a image file ? especially the beginning. file_path = dir(''); fid = fopen('');
Can you make up some example for me ? thanks
댓글 수: 4
Michael Haderlein
2014년 8월 6일
This code looks incomplete. The first line doesn't make much sense as it is written. dir() returns file/folder names if they match with the argument. So, dir('example*.txt') will return a structure with all files example1.txt, example2.txt and so on (if they exist in the current directory). You can then open them by fopen, such as fid=fopen(file_path(1).name); The '' argument will not open a file and just return -1 (file not found).
In case you get a valid file identifier, you can scroll through the file by the while loop, however, an end is missing here. The initialization lines do not make sense here as they will be rewritten to [] every iteration and not even be touched. The fgetl lines read one line of the file. So first, you read the pattern_size, then the pattern_hour and so on. You then try to find the file named as file_path. Maybe this file should be opened in further lines, but that's not included here.
Hope I could shed some light on this code.
tabw
2014년 8월 6일
dpb
2014년 8월 6일
What's missing?
doc imread
for the function.
채택된 답변
추가 답변 (2개)
Image Analyst
2014년 8월 6일
0 개 추천
And fclose() is also missing. This will only read an image file if the image file was written out as ASCII text. Otherwise you should use fread() rather than fgetl() to get the pixels out. And that's only if it's some custom format that imread() doesn't know about.
댓글 수: 3
tabw
2014년 8월 7일
dpb
2014년 8월 7일
Please don't make same comments more than once't--_*most*_ confusing about what is/isn't answered...
Image Analyst
2014년 8월 7일
Don't use dir() in that case. Just say
folder = pwd; %'C:\Users\user\Desktop'
filePattern = sprintf('%s/*.PNG', folder) % Find PNG image files.
fileNames = dir(filePattern)
for k = 1 : length(fileNames)
thisBaseFileName = fileNames(k).name;
thisFullFileName = fullfile(folder, thisBaseFileName);
fprintf('Now processing %s...\n', thisFullFileName);
end
...IF the file is not in current directory...
Then include it in the argument to dir, of course...
file_path = 'C:\Users\user\Desktop';
d=dir(fullfile(file_path,'text*.txt'));
for i=1:length(d)
fid=fopen(fullfile(file_path,d(i).name));
...
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!