How to get struct results in a loop?

I have a function that reads a number of files with the extension .m and turns them into inline functions in a struct. The purpose of the function is that the user can load as many models as he wants and the program will always respond with the correct number of entries.
filePattern = fullfile(myFolder, 'model_*.m'); % Only files with prefix and suffix defined in this project
theFiles = dir(filePattern);
FnCarbona = struct; %Creates a structure that will receive function pointers in files
for k = 1 : length(theFiles) % This is from another post here in Matlab Answer
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Lendo o modelo .... %s\n', fullFileName);
newStr = regexprep(baseFileName,'.m','');
FnCarbona(k).ponteiro= str2func(newStr);
FnCarbona(k).nome = newStr;
end
[n, m]= size(FnCarbona);
This part works pretty well. If I run individually, I get the vectors correctly (5 vector 1x50 elements)
The problem is when I try to access this in a (for) loop.
data=zeros(m,50);
for i= 1: m
data(i)= FnCarbona(i).ponteiro();
end
Then I get the error message: Unable to perform assignment because the left and right sides have a different number of elements.
Could someone help me? I want

1 个评论

Note that rather than creating an entirely new structure array FnCarbona (and expanding it on each loop iteration), you can simply use the already-existing theFiles structure array.

请先登录,再进行评论。

 采纳的回答

data(i,:) = FnCarbona(i).ponteiro();
% ^^ indexing needs to specify what happens to all 50 elements.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by