Hello everyone, i want to load this 3 set of data files into matlab using for loop but it is not working for me. Any help...............
clear all;
close all;
clc;
datafile={'data1.mat' 'data2.mat' 'data3.mat'};
num_schemes=3;
for sno=1:num_schemes
files = char(data(sno));
load files
end

 Respuesta aceptada

Stephen23
Stephen23 el 7 de Feb. de 2015
Editada: Stephen23 el 16 de Mzo. de 2021

5 votos

You are getting confused between the command syntax and the function syntax. In particular you are trying to use the command syntax with a variable named files, which will never work as the line of code
load files
is interpreted as
load('files')
Where 'files' is treated as a string, not a variable. This is specifically explained in the load documentation: Do not use command form when any of the inputs, such as filename, are variables. The easiest solution is do not use the command syntax in your code, only use the function syntax and then you will never face this problem again. Try this instead:
filenames = {'data1.mat','data2.mat','data3.mat'};
for kk = 1:numel(filenames)
S = load(filenames{kk}) % Best to load into an output variable.
..
end
Also note that I fixed the cell array indexing to use the correct {} braces , removed the char operation which is completely unnecessary as the contents of the cell array filenames are already of class character, and for robustness loaded the file data into an output variable (which is a scalar structure).

8 comentarios

Aftab Ahmed Khan
Aftab Ahmed Khan el 7 de Feb. de 2015
Thanks, it works.
AUWAL ABUBAKAR
AUWAL ABUBAKAR el 21 de Nov. de 2019
Wow! Thanks alot Stephen. it works fine for me.
please how about if I have upto 500 files. must I type all the 500 file names?
Is there any way I can cut it short?
Thanks in advance
Stephen23
Stephen23 el 21 de Nov. de 2019
"must I type all the 500 file names?"
No.
"Is there any way I can cut it short?"
Either use dir or sprintf, as the MATLAB documentation shows:
Wiqas Ahmad
Wiqas Ahmad el 16 de Mzo. de 2021
filenames = {'data1.mat','data2.mat','data3.mat'};
for kk = 1:numel(filenames)
load(filenames{kk})
end
Suppose in this case if I have three variables, for instance a,b and c in each data file and I want to execuate all these variables in my program from all data files, How can I code the program?
Stephen23
Stephen23 el 16 de Mzo. de 2021
Editada: Stephen23 el 16 de Mzo. de 2021
@Wiqas Ahmad: load the file data into an output variable (which is a scalar structure):
F = {'data1.mat','data2.mat','data3.mat'};
N = numel(F);
C = cell(1,N);
for kk = 1:N
C{kk} = load(F{kk});
end
S = [C{:}];
You can access the data using indexing and fieldnames, e.g. the data from the second file:
S(2).a
S(2).b
S(2).c
Arif Hoq
Arif Hoq el 17 de Dic. de 2021
Thank you very much @Stephen. it helped me a l lot. i have another query according to this.
S(2).a
S(2).b
S(2).c
above code can find the a,b,c from only file no 2.
  1. any solution to get these variables a,b,c from all files in 1 matrix ? and
  2. only variable 'a' from all 3 files?
Stephen23
Stephen23 el 17 de Dic. de 2021
@Mohammad Ariful Hoq: yes, use comma-separated lists:
For example, to get the 'a' variable from all files into one array:
a = [S.a] % or VERTCAT or HORZCAT
Arif Hoq
Arif Hoq el 20 de Dic. de 2021
thank you very much@Stephen

Iniciar sesión para comentar.

Más respuestas (1)

ELCIO S
ELCIO S el 30 de Oct. de 2018

1 voto

What about if I have many files with different names? How can I do to load all of them?

1 comentario

Stephen23
Stephen23 el 30 de Oct. de 2018
Editada: Stephen23 el 17 de Dic. de 2021
"What about if I have many files with different names? How can I do to load all of them?"
Use DIR or SPRINTF:

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Preguntada:

el 7 de Feb. de 2015

Comentada:

el 20 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by