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

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

Related

plotting results from different files into on figure by using different colors [duplicate]

This question already has answers here:
Plot different colors
(2 answers)
Closed 5 years ago.
I have some log files and in these log files there are some timestamps from different smartphones. I plotted each log file into different figures. Lets say there are 3 smartphones and each has a specific number like 10, 11, 12 and each smartphone's result keeps in one log file.
Basically what I want to do is, showing the results of these three log files into one figure by using different colors for each log file. Is there anyone who knows how to do it?
EDIT
n=size(allTimeStamps{1},2);
figure(1);
hold on;
for i=1:n
plot(allTimeStamps{1}{i},mod(allTimeStamps{1}{i},0.3),'Color',colorspec{indexOfFile});
end
title(logFileName);
You can use the function lines to get the default colors of Matlab in order. This function creates a matrix of n-by-3 that each row within is a new color. However this is good up to 7 colors, otherwise, you can choose another colormap, or use this suggestion.
Here is an example:
data = reshape(1:99,[],3); % some arbitrary data
n = size(data,2);
figure;
hold on;
col = lines(n);
for k = 1:n
plot(data(:,k),'Color',col(k,:));
end
hold off
(This code is only for demonstrating, in this specific case you don't even need a loop, because plot(data) will give the same result)

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

MATLAB: How can I efficiently read in these data files?

I have 100 data files in a folder called "Experiment1", and I need to take all of the data from them and put them into a single matrix. Each data file contains 15 columns and 40 rows of data.
The order in which the files are in the folder is arbitrary. It doesn't matter in what order they get put into the combined matrix.
I've written some code using dlmread that will do the job:
for i = 1:100
%% Read in the relevant file.
filename = ['File_' int2str(i) '.dat']
Data = dlmread(fullfile(pwd, 'Experiment1',filename));
%% Put all the data I need in a separate matrix
NeededData(1+((i-1)*40):i+((i-1)*40)-i+40,1:15) = Data(:,1:15);
end
However, there are two things I don't like about my code.
The files have random names at present, and I'd need to manually change all their names to "File_1.dat", "File_2.dat", etc.
The code is cumbersome and hard to read.
How could I do things better?
Since you've fixed the problem of defining the name of the files to be read with dir, you can improve the way you add the read data (Data) to the output matrix (NeededData).
You can sumultaneously read the input files and add the data to the output matrix by inserting the call to dlmread directly in the assignment statement:
files=dir('*.dat');
n_files=length(files)
% Initialize the output matrix as empty
NeededData_0=[]
for i=1:n_files
% Simultaneously read input file and assign data to the output matrinx
NeededData_0=[NeededData_0;dlmread(files(i).name)]
end
In case you prefer working with the inides (as in your origina approach), since you know in advance that all the files have the same (40) number of rows) you can simplify the notation as follows:
files=dir('*.dat');
n_files=length(files)
% Define the number of rows in each inout file
n_rows=40;
% Define the number of colums in each inout file
n_col=15;
NeededData_2=nan(n_rows*n_files,n_col)
% Define the sequence of rows
r_list=1:n_rows:n_rows*n_files
for i=1:3
Data=dlmread(files(i).name)
NeededData_2(r_list(i):r_list(i)+n_rows-1,:)=Data
end
Hope this helps.
Using the suggestion to use dir present in the answers I have made the following code, which is clearly an improvement on my earlier effort. I would welcome further improvements, or suggestions for alternative approaches.
files = dir('*.dat');
for i = 1:length({files.name})
%% Read in the relevant file.
Data = dlmread(files(i).name);
%% Put all the data I need in a separate matrix
NeededData(1+((i-1)*40):i+((i-1)*40)-i+40,1:15) = Data(:,1:15);
end

how to select or load between different tables in workspace

I have a *.mat file with some tables included.I want to load each table one by one and then plot it so that the figures of each table update on one single figure .Please tell me if i am not clear ...I though this code must have been enough but it is not working when i asked it to load one table ... please guide me why and how i should improve the code ?
Thank you
filename = uigetfile
load(filename)
c=who('Hs*' ) % all the tables which starts with Hs
cc=numel(c) % count the number of desired tables in MAT file which we loaded before
for i=1: cc
b=load(filename,c(i));
contour(b,60)
end
I can see at least three errors in your code.
After the first load you don't clear the mat file, and hence all tables are still in the memory.
c is a cell array, and you shoud index it by using brackets (i.e. c{i}).
By default each plot in a figure deletes the previous one. Writing figure(); hold on; before the for loop shoud fix your problem.

MATLAB: how to get the list of figures [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I get the handles of all open figures in MATLAB
The situation is following. I run a couple of tests, which plot a lot of figures. I would like to convert them to pdf and compile into one file. Since each time I may get different type of plots and different number of plots, I need to get the list of all figures in current matlab session or workspace. Is this doable?
Thanks
h = get(0,'Children');
will put the "handles" to the figures you currently have in the variable h. get(handle) and set(handle,...) are gigantically useful in general. The handle 0 points at the root of the display, so all the figures on the display are the root's Children.