How to read multiple images using a loop in MATLAB? [duplicate] - matlab

This question already has answers here:
How to read mutliple images in a for loop in MATLAB?
(2 answers)
Closed 6 years ago.
I have a folder with the images that I want to read into MATLAB and perform various built-in functions and matrix operations to those input images.
Could anyone possibly help me figure it out?

assuming your images are .jpg:
files = dir('directory*.jpg');
for i=1:length(files)
images{i} = imread(files(i).name);
end
reads all images in current directory into cell array images
this one reads from directory:
directory = 'path to your directory';
files = dir([directory '/*.jpg']);
for i=1:length(files)
images{i} = imread([directory '/' files(i).name]);
end

Related

save the file with loop index in matlab [duplicate]

This question already has answers here:
How to save each file in a for loop using matlab
(1 answer)
save each data in loop as text file in matlab
(1 answer)
Save images in a loop with variable file names?
(2 answers)
Closed 10 months ago.
I want to save some variables in matlab program like
for i =1:10
x(i)=randn()*5;
save(i,x(i));
end
At every iteration 'i' I want it save the values of x(i), but matlab gives me the error "Error using save Must be a string scalar or character vector." How can I solve this

Read multiple csv files in MATLAB [duplicate]

This question already has answers here:
Loading files in a for loop in matlab
(1 answer)
Loop for loading and saving .mat files
(1 answer)
Load an do operations with a matrix whose name varies within a Matlab loop
(1 answer)
Loading a series of text files at matlab
(2 answers)
Closed 2 years ago.
I would like to read several CSV files in MATLAB.
The names of the files are as follows:
data_00001.csv,
data_00002.csv,
...,
data_00010.csv,
data_00011.csv,
...,
data_00100.csv,
data_00101.csv,
...,
data_01000.csv,
data_01001.csv,
...
I would like to know how I can update the name (i.e., number of zeros in the csv file name) and read these files using "for" loop in MATLAB.
You can use sprintf and readmatrix:
for i = 1: 10000
name = sprintf ('data_%0.5d.csv', i);
mat = readmatrix (name ); % or mat = csvread (name );
% ...
end

Loop over the names of files and apply a function on them in matlab [duplicate]

This question already has answers here:
Load all the images from a directory
(4 answers)
Closed 6 years ago.
I have folder containing thousands of (*.jpg) images inside and would like to loop over the name of them and apply calculations on them.
has anybody kind of loop in mind for this?
Loop over the variables that are determining your filenames and use sprintf() to format those variables into strings.
I can't quite figure out your filename pattern, but you would read the first group like this, for example:
for i = 0:9
% The %d character will be replaced by i in the string
filename = sprintf('abcda0b%d99.jpg',i);
im = imread(filename);
% Image calculations
end
If your filenames obey a single pattern, you can do this with nested loops to construct the filename from the variables that are determining your filename. If you reply with more detail about your naming pattern I can help you out.
You can use dir function:
path='The_path_to_directory_contains_the_jpg_files';
d=dir(path);
for k=3:length(d)
im=imread(fullfile(path,d(k).name));
% do calculations
end

Reading an image in MATLAB [duplicate]

This question already has answers here:
How to read images from folders in matlab
(3 answers)
Closed 6 years ago.
I want to load an image in MATLAB :
f=imread('fulldirectory');
m=size(f);
printf('m');
MATLAB shows me this error when I attempt to run it:
"Error using imread (line 349)"
Can anyone help me?
Imread function needs a file format, e.g., jpg.
You can use one of the following MATLAB codes.
f = imread('ngc6543a.jpg');
f = imread('ngc6543a', 'jpg');
Also, just omit the semicolon (;) from your second line of your codes, if you want to know the dimension of the image. MATLAB will display the size of the image.
m=size(f)
MATLAB has some test images such as ngc6543a.jpg and you can try my code in your MATLAB.

Loading (and computing/plotting) multiple .MAT files [duplicate]

This question already has answers here:
How can I load 100 files with similar names and/or string in just one step in MATLAB?
(3 answers)
Closed 6 years ago.
I have 4 .MAT files that I need to run similar functions on, and plot on same graph. Problem is, if I load first file, it only runs on that file. After the "load" function, there are 163 lines of code to repeat. Some answers I have seen require .Mat files with similar naming convention.
File names are:
M1_N_o
M2_S_o
M3_N-b
M4_S_b
Only a little info is given. If you could provide the code it will be more helpful. So I am assuming a lot of stuffs.
I am assuming that all files have the same variables with same dimensions
First rename files
M1_N_o.mat,M2_S_o.mat,M3_N-b.mat,M4_S_b.mat
to
M1.mat,M2.mat,M3.mat,M4.mat
Matlab Code:
figure
hold on
numberOfFiles=4;
for fileIndex =1:numberOfFiles
fileName=strcat('M',num2str(fileIndex),'.mat');
load(fileName);
% your 163 lines of code
% do your plots
end
hold off
If you dont want to rename the files then
figure
hold on
fileNames={'M1_N_o.mat' ;'M2_S_o.mat'; 'M3_N-b.mat'; 'M4_S_b.mat'}
for fileIndex =1:size(fileNames,1)
load(fileNames{fileIndex});
% your 163 lines of code
% do your plots
end
hold off