Home > Net >  Dynamically calling gather function in MatLab
Dynamically calling gather function in MatLab

Time:11-02

I am trying to dynamically call the gather() function in MatLab.

I am currently doing something like so:

for index_1 = 1:1:document_count
  current_filename = "random_file" index_1 ".mat";
  data = load(current_filename);
  pci= gather(datasets.current_filename.pci.dist);
end 

In the above, the data is loaded however the gather function fails, I assume because I am passing it a String in the middle. I am not sure what a workaround could be.

Any advice would be greatly appreciated.

CodePudding user response:

You need to place the string (current_filename) in parenthesis when using it as a field name of a struct. See here.

for index_1 = 1:1:document_count
   current_filename = "random_file" index_1 ".mat";
   data = load(current_filename);
   pci= gather(datasets.(current_filename).pci.dist);
 end

That said, I am guessing you will need to drop the extension (.mat) off of your filename for this to work. For example, do you really have datasets.random_file1.mat.pci.dist?

CodePudding user response:

The way in which I did it was:

fields = fieldname(data)
data = getfield(data,fields{1})

I used this technique to iterate through the subfields

pci_field = getfield(data, fields{1})
pci = gather(pci_field.dist)
  • Related