save the file with loop index in matlab [duplicate] - matlab

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

Related

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

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

Save results in a nested for loop [duplicate]

This question already has answers here:
Save loop data in vector
(2 answers)
Closed 7 years ago.
I want to save the following for-loop results in a new matrix using Matlab, how can I do that or any other suggestions?
Where X is a 5467-by-513 matrix , id is a 143-by-1 vector and wkno is a 44-by-1 vector
for i=1:size(id,1);
for j=1:size(wkno,1);
tst= X(:,1)==id(i) & X(:,2)==wkno(j);
M=mean(X(tst,:));
end
end
Just make sure you actually save the things to a matrix instead of a scalar-variable, i.e. add the subscript indices to the variable you're saving to:
for ii=1:size(id,1);
for jj=1:size(wkno,1);
tst(ii,jj)= X(:,1)==id(ii,1) & X(:,2)==wkno(jj,1);
M(ii,jj)=mean(X(tst,:));
end
end
Not that I refrained from using i and j as a variable, since this is a bad idea. I added the ,1 to id and wkno, to make sure you use them as column variables. This is a good habit to get into, because single indices will go wrong when you have a multi-dimensional array.

significance of ~ symbol in matlab function [duplicate]

This question already has answers here:
suppressing output variables in matlab
(2 answers)
Closed 8 years ago.
If I have a function a that accepts 2 parameters (double) in Matlab as follows
function [x,y] = a(z)
What does the symbol "~" do when the function is called with this handle as follows
[x,~,y] = a[10]
Thanks
The "~" symbol in matlab is logical NOT. So it's basically like ignoring that output/input. For example, if I have a line of code like this:
[out1,~,out3] = function(vargin);
the second output is not kept or stored anywhere for later use. For more info, type "help ~" in the command window.

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