How to iterate through the struct char to label a new struct correctly?

3 ビュー (過去 30 日間)
Goncalo Costa
Goncalo Costa 2023 年 2 月 22 日
編集済み: Stephen23 2023 年 2 月 22 日
I know that when I create a structure, e.g.
fruits(1).apples = data1;
fruits(1).bananas = data2;
I obtain a structure with two fields, and each fields having their respective data1 and data2. What I am attempting to do is loosely based on this.
The same way that above I have labelled my fields "apples" and "bananas", I have a 9x1 field with the names of what I shall label my data fields (shown in the code below).
% List of all the desired files in the folder
%the names of my files are: sample_1, sample_10, sample_13, etc.
filePattern = fullfile(myFolder, 'sample*.mat'); %search for files
theFiles = dir(filePattern);
theFiles = natsortfiles(theFiles); %arrange the files names in numerical order
%the data is a 1x30 structure
for x = 1 : length(theFiles)
baseFileName = theFiles(x).name; %e.g. for x =1 , baseFileName = 'sample_1.mat'
data(1).baseFileName =nk('ref_K15rapidscan.mat','base_K15rapidscan.mat',baseFileName);
end
%nk is a function to process data and to give other data back. The data resulting from this is a 1x30 struct with 2 fields
The problem is that I cannot simply write "data(1).baseFileName", as MATLAB doesn't recognise that baseFileName is a different char each time on the last line
data(1).baseFileName =nk('ref_K15rapidscan.mat','base_K15rapidscan.mat',baseFileName);
How can I end up with something like the image below?
(The image might make it look like I actually got it to work, but really I just changed the name of the fields (apple, pear banana, etc) by hand to sample_1, etc.
  2 件のコメント
Image Analyst
Image Analyst 2023 年 2 月 22 日
What is this nk function or array you're using???
Goncalo Costa
Goncalo Costa 2023 年 2 月 22 日
This is just a function written by a colleague to process some data and give me some data back as a result. The data resulting from this function is in the form of a 1x30 struct with 2 fields as shown in the last image (I will rewire this onto the questions).

サインインしてコメントする。

採用された回答

Goncalo Costa
Goncalo Costa 2023 年 2 月 22 日
I ended up solving it and I am placing here my answer:
% List of all the desired files in the folder
filePattern = fullfile(myFolder, 'sample*.mat'); %search for files
theFiles = dir(filePattern);
theFiles = natsortfiles(theFiles); %name of files and other info
%all data placed into a single struct w/ x fields
for x = 1 : length(theFiles)
baseFileName = theFiles(x).name;
[path, name, ext] = fileparts(theFiles(x).name);
data(1).(name) =nk('ref.mat','base.mat',baseFileName);
end
  2 件のコメント
Voss
Voss 2023 年 2 月 22 日
編集済み: Voss 2023 年 2 月 22 日
Stephen23
Stephen23 2023 年 2 月 22 日
編集済み: Stephen23 2023 年 2 月 22 日
Not that this is fragile data design: there are many characters which are valid in filenames which are not valid in fieldnames. When you get such a character, this code will fail.
For much more robust code which does not duplicate data in extra variables, see the answer I gave you earlier:

サインインしてコメントする。

その他の回答 (2 件)

Oguz Kaan Hancioglu
Oguz Kaan Hancioglu 2023 年 2 月 22 日
You can use eval function to use char arrays or strings as a matlab command.
data(1).baseFileName =nk('ref_K15rapidscan.mat','base_K15rapidscan.mat',eval(baseFileName));
  1 件のコメント
Goncalo Costa
Goncalo Costa 2023 年 2 月 22 日
When I use this it tell me the following error
Unable to resolve the name sample_10.mat.
I think I need to separate the .mat from the rest of the files names, I tried to do this using fileparts, but it doesn't work either.

サインインしてコメントする。


Voss
Voss 2023 年 2 月 22 日
for x = 1 : length(theFiles)
baseFileName = theFiles(x).name; %e.g. for x =1 , baseFileName = 'sample_1.mat'
[~,fn,~] = fileparts(baseFileName);
data(1).(fn) = nk('ref_K15rapidscan.mat','base_K15rapidscan.mat',baseFileName);
end

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by