Import or Export a Sequence of Files

I have been trying to export photos on MATLAB. With a.bmp, b.bmp and c.bmp works perfectly, but wrting *.bmp gives me an error. Could you help me out please.
Thanks
clc; clear all;
% paths = fullfile('c:\','users','fenec','Desktop','Cam_1',{'a.bmp'; 'b.bmp'; 'c.bmp'});
paths = dir(fullfile('c:\','users','fenec','Desktop','Cam_1','*.bmp'));
for R = 1:length(paths)
temp = char(paths(R));
I = imread(temp);
figure;
imshow(I, []);
end

2 Comments

I don't see how this approach would ever work without error:
S = dir();
char(S(1))
Error using char
Conversion to char from struct is not possible.
Most likely you would be much better off just using the standard approach of using indexing and fieldname to access the structure content (just as they were designed for):
Thats why I got confused. Since when uncommenting the 2nd line:
clc; clear all;
paths = fullfile('c:\','users','fenec','Desktop','Cam_1',{'a.bmp'; 'b.bmp'; 'c.bmp'});
% paths = dir(fullfile('c:\','users','fenec','Desktop','Cam_1','*.bmp'));
for R = 1:length(paths)
temp = char(paths(R));
I = imread(temp);
figure;
imshow(I, []);
end
Like the above code works perfectly.
The problem when came when I wanted to be more generic, by having
'*.bmp'
Thanks for link provided.
Moreover, the answer and explanation to this query can be found here:
https://www.mathworks.com/matlabcentral/answers/757974-import-or-export-a-sequence-of-files#answer_635024

Sign in to comment.

 Accepted Answer

Perhaps the issue is that paths is a structure containing several fields and not just the path. Look at the dir documentation page for more.
The fields are
  • name
  • folder
  • date
  • bytes
  • isdir
  • datenum
See explanations here.
Perhaps modify your code to be
for R = 1:length(paths)
temp = fullfile(paths(R).folder,paths(R).name);
I = imread(temp);
figure;
imshow(I, []);
end

4 Comments

Luca Fenech
Luca Fenech on 27 Feb 2021
Edited: Luca Fenech on 27 Feb 2021
Wow this worked perfectly! Thanks for the explanation and code provided. Much appreciated.
Sorry to interrupt you again @Cris LaPierre, but while working with the code which you provided, I am noticing that although pictures are being uplaoded to MATLAB, they are not coming one after the other as they are in the folder. Do you have any idea of how I can solve this issue please?
Just to tell you the images are named as 'image_0', 'image_1', 'image_2' etc.
This is the code that I am working with:
clc; clear all;
paths = dir(fullfile('c:\','users','fenec','Desktop','Cam 1','*.bmp'));
for R = 1:length(paths)
temp = fullfile(paths(R).folder, paths(R).name);
I = imread(temp);
figure;
imshow(I, []);
end
@Luca Fenech: download, unzip, and use NATSORTFILES (following its documentation of course):
For example:
P = fullfile('C:\','users','fenec','Desktop','Cam 1');
S = dir(fullfile(P,'*.bmp'));
C = natsortfiles({S.name}); % sort filenames alphanumerically.
for k = 1:numel(C)
F = fullfile(P,C{k});
I = imread(F);
figure;
imshow(I,[]);
end
You can also sort the structure S by using the second output from NATOSRTFILES, which is the sort index.
@Stephen Cobeldick... this worked perfectly.
I really appreciate for your help. Thanks a lot.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!