Read multiple csv files in MATLAB [duplicate] - matlab

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

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

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

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

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

MATLAB find the point of a value obtained using min [duplicate]

This question already has answers here:
How do I get the index of the smallest element in an array in matlab?
(2 answers)
Closed 8 years ago.
I want to match a time to the minimum value of TEMP found in an array that I'm reading into a struct from a netcdf file in a loop (that's doing a lot more stuff - no I don't want to get rid of the loop). I have the snctools so that's what I'm using for netcdfs.
Here's my current relevant lines of code:
%Get the netcdf file with file_string loading into MATLAB
nc=netcdf(file_string);
%Work out the number of files I need to loop through
[files]=dir('*.nc');
max1=length(files);
for d1=1:max1
%extract the TEMP 1-D array
B1=nc{'TEMP'}(:)
%assign to value
dat.TEMP_min(d1)= min(B1);
end
Now there is another variable of the same length called 'TIMES'. If min(B1)=10.5 and is the nth element of B1 then I want to locate the nth element of TIMES and save it as dat.TEMP_min_TIME. How would I go about this?
Please provide enough notes on any code so that a novice can understand it.
You can just use inside your loop (sorry for pseudocode, not sure about your definitions of n and TIMES)
[M,I] = min(B1);
dat.TEMP_min(d1)= M;
if (I == n)
dat.TEMP_min_TIME = nc1{'TIMES'}(I); %
end

Exporting HDF4 data in Matlab [duplicate]

This question already exists:
Matlab HDF Files [closed]
Closed 8 years ago.
I need a script to export data from Matlab to HDF4 format. The variable which I want to store in hdf4 file has dimensions 3128*242*256 (int 16 type).
Thanks
use following routines:
x = ones (3128, 242, 256) ;
hdf5write ('file path', 'dataset name' , x )
Above code will create hdf5 file and containing a dataset having 3D matrix containing values 1.