How to iterate over a char array and store them into a separate array?
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am developing a code that iterates over files with different names, and then processes the data of each file, one at a time.
To do so, I started by writing the following
myFolder = 'D:\Code\Work\Data';
% List of all the desired files in the folder
filePattern = fullfile(myFolder, 'sample*.mat'); %search for files with the word sample in them
theFiles = dir(filePattern);
for x = 1 : length(theFiles)
baseFileName = theFiles(x).name;
data =nk_process('ref.mat','base.mat',baseFileName);
fullFileName = fullfile(theFiles(x).folder, baseFileName);
end
Each data is a 1x30 struct with 2 fields, and the way the code is written the data struct is overwritten every iteration.
I want to be able create an array for each of these data structs and to be able to identify which 'sample*.mat' file they correspond to.
To do so, I thought that I could start by writing the bottom half of the code above simply as
for x = 1 : length(theFiles)
baseFileName(x) = theFiles(x).name;
data(x) =nk_process('ref.mat','base.mat',baseFileName(x));
fullFileName(x) = fullfile(theFiles(x).folder, baseFileName);
end
But this doesn't work due to the the class of the theFiles(x).name files being char.
How can I overcome this problem? And how can I afterwards link each data struct to their corresponding mat files?
PS: In case it makes things clearer, the files are called: latex_0.mat, latex_5.mat, latex_15.mat and so on.
Thanks for any help.
0 comentarios
Respuestas (1)
Stephen23
el 22 de Feb. de 2023
Editada: Stephen23
el 22 de Feb. de 2023
"I want to be able create an array for each of these data structs ..."
The simple approach is to use the structure array which you already have to store that data:
P = 'D:\Code\Work\Data';
S = dir(fullfile(P,'sample*.mat'));
S = natsortfiles(S); % optional, see text.
for k = 1:numel(S)
S(k).full = fullfile(S(k).folder, S(k).name);
S(k).data = nk_process('ref.mat','base.mat', S(k).full);
end
"...and to be able to identify which 'sample*.mat' file they correspond to."
Now we have everything in one structure that is very easy. For example, the 2nd file:
S(2).name % just filename
S(2).full % full filename
S(2).data % your data
If you expect the files to be listed in numeric order then you will need to sort them, e.g. by downloading the function NATSORTFILES() and using it to sort the structure returned by DIR():
2 comentarios
Stephen23
el 22 de Feb. de 2023
Editada: Stephen23
el 22 de Feb. de 2023
"I wanted to get these structure themselves into another structure. "
That is exactly what I gave you: all of your structures are in the structure S, and the filedata and filenames, etc. can be trivially accessed by clicking on that structure in the Variable Viewer.
"So that I could obtain something like two columns (9x2), where the left column would be the 9 file names and the right one would be the 9 structures correponding to the data shown in the image (on which I'd click to get the image above)."
What you describe is more like a cell array, not a structure. You can easily create a cell array too:
C = {S.name;S.data}.'
That gives you exactly what you describe: two columns, the 1st with names and the 2nd with those structures.
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!