How to iterate over a char array and store them into a separate array?

23 Ansichten (letzte 30 Tage)
Goncalo Costa
Goncalo Costa am 22 Feb. 2023
Bearbeitet: Stephen23 am 22 Feb. 2023
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.

Antworten (1)

Stephen23
Stephen23 am 22 Feb. 2023
Bearbeitet: Stephen23 am 22 Feb. 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 Kommentare
Goncalo Costa
Goncalo Costa am 22 Feb. 2023
Thank you for your help, and although this helps me sort out the data I am still not fully able to do what I wanted, but perhaps I need to reword it better, as I don't know quite how to explain it.
In each iteration I get something like the image below
This the data I get in my code.
I wanted to get these structure themselves into another structure. 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).
This is possibly a really confusing way of wording things, but I don't know how to explain it very concisely.
Thank you for your help.
Stephen23
Stephen23 am 22 Feb. 2023
Bearbeitet: Stephen23 am 22 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by