Read multiple .mat files in the file path in sequence

Hi everyone,
There are 10 .mat files in the file path. I need to read the value of one matrix (called 'Scanning') in this file and keep it in another.
For instance ,
0_cm(1).mat , 0_cm(2).mat .... 0_cm(10).mat
I get the error when I write the following matlab code and filename is a string type, so I can't access files with the variable i.
Test = [];
for i=1:10
filename = "D:\matlab\0_cm(i).mat";
myVars = {'Scanning'};
S = load(filename,myVars{:});
Test = S.Scanning(:,64)
end
Thanks for your answer in advance.

Respuestas (2)

dpb
dpb el 30 de Ag. de 2019
for i=1:10
filename = sprintf('D:\matlab\0_cm(%d).mat',i); % build specific file name
myVars = {'Scanning'};
S = load(filename,myVars{:});
Test = S.Scanning(:,64)
end

3 comentarios

Stephen23
Stephen23 el 30 de Ag. de 2019
Editada: Stephen23 el 30 de Ag. de 2019
@dpb: this will throw an error (due to the invalid \m formatting operator):
sprintf('D:\matlab\0_cm(%d).mat',i)
For example:
>> sprintf('D:\matlab\0_cm(%d).mat',4)
Warning: Escape sequence 'm' is not valid. See 'help sprintf' for valid escape sequences.
Also: most likely you did not intend to create a null character (although that is what you did with the formatting operator \0).
You could:
  • escape the backslashes
  • use forward slashes
  • include the path as a separate input
  • join the path afterwards using fullfile.
You could also use string with the + operator.
n = 9;
filename = "D:\matlab\0_cm(" + n + ").mat";
You could use this with fullfile if you wanted.
n = 9;
FN = "0_cm(" + n + ").mat";
filename = fullfile('D:\', 'matlab', FN)
@dpb, yes an error is emerging as @Stephen Cobeldick mentions.
Warning: Escaped character '\m' is not valid. See 'doc sprintf' for supported special characters.
Thanks @Stephen Cobeldick,

Iniciar sesión para comentar.

mehmet irfan gedik
mehmet irfan gedik el 30 de Ag. de 2019
Editada: mehmet irfan gedik el 30 de Ag. de 2019
Thanks @Steven Lord ,
We could actually solve the problem with a fairly simple method.
Edit : But I wonder why the sprintf function is causing such a problem ???.

2 comentarios

The reason there is a problem is that when compiling sprintf into a string, the function uses the '\' as an escape character as stephen mentioned. What this means is the text interpreter will look at the next charactes expecting not text but some indicator for something else.
For example, if you want a tab in your string you can do '\t', or '\n' for a newline. Since the '\' is being used like that matlab puts in a way to keep it. typeing '\\' results in a single backslash in the string. So the code can be fixed using stephen's "escape the backslash" method as such
filename = sprintf('D:\\matlab\\0_cm(%d).mat',i);
@David K. , Thank you for your descriptive sentences.

Iniciar sesión para comentar.

Productos

Preguntada:

el 30 de Ag. de 2019

Comentada:

el 30 de Ag. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by