How do I access a folder that is inside the current folder while running a function?

I am writing a function that in different steps needs to access 1 or 2 files in different folders. The function will look something like this:
function Calculation = calc(n)
where n is the name of the file. Each of the folders will have one file named "N" and I want the program to go through each file and get the image data for that file.

2 件のコメント

Walter Roberson
Walter Roberson 2015 年 7 月 25 日
Is n the name of the folder or the name of the file or the name of the combination?
Sulaiman Alvi
Sulaiman Alvi 2015 年 7 月 25 日
n is the name of the file. there is an "n" file in each of the folders that I am trying to access and I want to access each of them.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2015 年 7 月 25 日
Presuming that n holds the name of the folder
this_N_name = fullfile(n, 'N'); %N has no extension??
this_data = imread(this_N_name); %imread is not going to be happy with there not being an extension though

6 件のコメント

Sulaiman Alvi
Sulaiman Alvi 2015 年 7 月 25 日
the extention for all of these files is .bmp
function all_data = fetch_data(n)
dirinfo = dir('*'); %find everything
dirinfo = dirinfo([dirinfo.isdir]); %select only directories
file_count = 0;
for K = 1 : length(dirinfo)
this_file = fullfile(dirinfo(K).name, n);
if exist(this_file, 'file')
try
this_data = imread(this_file);
file_count = file_count + 1;
all_data{file_count} = this_data;
catch ME
fprintf('file "%s" was not valid image file, skipped it', this_file);
end
end
end
end
Returns a cell array of images, which you could then loop through to do your calculations. You would need to change it slightly if you want the file names returned along with the content. This code does not warn for directories that do not have a copy of the file, only for cases the file exists but could not be read as an image.
Sulaiman Alvi
Sulaiman Alvi 2015 年 7 月 25 日
Ok thanks a lot. How do I retrieve the cell array?
For example,
retrieved_data = fetch_data('Q39.bmp');
for K = 1 : length(retrieved_data)
imshow(retrieved_data{K});
pause(0.05);
end
Hey. I ended up writing my own code based on what you gave me. I wanted to test it but it didnt work. Can you take a look at it?
function I3value = I3(f1)
A = double(imread(f1,'bmp'));
I3value = (A);
imshow(I3value)
Above is what I want to do with the file.
Here is the rest of the code:
function nvalue = calcStokes(n)
full_name = fullfile('I0', n.bmp);
I3value = I3(full_name);
disp(I3value)
I0 is the folder i wanted to get it from, and I need to get a file with the name 15 and the bmp format. How can I fix this?
full_name = fullfile('I0', sprintf('%d.bmp', n));
By the way, when you are working with images, in a number of contexts MATLAB pays attention to whether the array is uint8 or double. When it is uint8 MATLAB expects the values to be in the range 0 to 255; when it is double MATLAB expects the values to be in the range 0 to 1. I recommend you use
A = im2double(imread(f1,'bmp'));
instead of using double()

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSearch Path についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by