hi everyone,
I have a question.
I am analysing some data which have multiple subjects. Information about every subject is stored in the MATLAB structure (.mat file)
For Example:
U1_Acc_TimeD_FreqD_FDay.mat
U2_Acc_TimeD_FreqD_FDay.mat
U1_Acc_TimeD_FreqD_FDay.mat
and so on...
I would like to create for loop which in every iteration load new data_number.mat file.
I'd like in every iteration to load next .mat file? Particularly, I don't know how to address number part of filename using load function.
I tried this code and seems it's overwritten the data!
clear;
for nc = 1:36
load(['U', num2str(nc,'%2d'), '_Acc_TimeD_FreqD_FDay.mat']);
end
Thank you

 Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Dic. de 2016

2 votos

load(['U_', num2str(nc,'%02d'), 'Acc_TimeD_FreqD_FDay.mat']);
or
load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );

10 comentarios

neamah al-naffakh
neamah al-naffakh el 28 de Dic. de 2016
dear Walter Roberson, thank you very much. Just a small correction, if you don't mind because your code gives a simple error due to the file name
for nc = 1:36
load(['U', num2str(nc,'%2d'), '_Acc_TimeD_FreqD_FDay.mat']);
end
Again really your answer was so useful. thank you mate.
neamah al-naffakh
neamah al-naffakh el 28 de Dic. de 2016
Editada: neamah al-naffakh el 28 de Dic. de 2016
dear Walter Roberson, this code is uploading only the one mat file!! could you help me please? seems is data is overwritten!
Walter Roberson
Walter Roberson el 28 de Dic. de 2016
Your question did not ask for the data to be stored separately.
for nc = 1 : 36
data{nc} = load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );
end
This will give you a cell array of structures, with each structure containing one field for each variable that is stored in the .mat file.
If you already know the name of the variable you want to extract, then,
for nc = 1 : 36
file_data = load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );
data{nc} = file_data.NameOfVariableGoesHere;
end
This code does not assume that the size or data type of the stored information is all the same. If you know that it is vectors that are consistent length, then you could use
for nc = 1 : 36
file_data = load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );
data(:,nc) = file_data.NameOfVariableGoesHere;
end
after which the data would appear as columns, one column for each file.
neamah al-naffakh
neamah al-naffakh el 28 de Dic. de 2016
Editada: neamah al-naffakh el 28 de Dic. de 2016
Dear Walter Roberson,
I really appreciate your kind help.
I am trying to access each Struct inside the cell (data) and extract specific rows and columns.
I have written this code but it stores the data of the last Struct only!
clear;
for nc = 1 : 36
data{nc} = load( sprintf('U%02d_Acc_TimeD_FreqD_FDay.mat', nc) );
end
for j=size(data,2)
Feat_Vec1=(data{j}.Acc_TD_Feat_Vec(2:36,1:126));
end
could you help me with this please?
Walter Roberson
Walter Roberson el 28 de Dic. de 2016
for j = 1 : size(data,2)
Feat_Vec1(:,:,j) = data{j}.Acc_TD_Feat_Vec(2:36,1:126);
end
This would create a 35 x 126 x 36 array
neamah al-naffakh
neamah al-naffakh el 28 de Dic. de 2016
Editada: neamah al-naffakh el 28 de Dic. de 2016
thank you so much, but actually, it doesn't give me the correct output!
*it converts the values of each struct into 0 :(*
please see
neamah al-naffakh
neamah al-naffakh el 28 de Dic. de 2016
when I use this code, I get what I want but only for the last struct (36) in the cell data
clear;
for nc = 1 : 36
data{nc} = load( sprintf('U%02d_Acc_TimeD_FreqD_FDay.mat', nc) );
end
for j=size(data,2)
Feat_Vec1{j} = data{j}.Acc_TD_Feat_Vec(2:36,1:126);
end
please have a look on here !!
neamah al-naffakh
neamah al-naffakh el 28 de Dic. de 2016
Editada: neamah al-naffakh el 28 de Dic. de 2016
I have solved it but it's really weird !!
clear;
for nc = 1 : 36
data{nc} = load( sprintf('U%02d_Acc_TimeD_FreqD_FDay.mat', nc) );
end
for j=1:36 *% instead of for j=size(data,2)*
Feat_Vec1{j} = data{j}.Acc_TD_Feat_Vec(2:36,1:126);
end
Walter Roberson
Walter Roberson el 28 de Dic. de 2016
Notice I had suggested
for j = 1 : size(data,2)
rather than
for j = size(data,2)
the second of those does only size(data,2)
neamah al-naffakh
neamah al-naffakh el 30 de Dic. de 2016
Editada: neamah al-naffakh el 30 de Dic. de 2016
dear Walter Roberson, many thanks for your help. I really appreciate it.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by