From: pass a struct through a loop to extract data and have the i in the loop added to the name
In this for loop:
for i = 1:length(pred_files)
for j = 1:length(n_unknown)
if predictions(1).pred_i.y_pred(j) == predictions(1).y_all(j)
accuracy_totals(1).accuracy_total_i = accuracy_totals(1).accuracy_total_i +1
end
end
end
I want i to be added to the struct name; for example, on the first iteration, predictions(1).pred_i would be predictions(1).pred_1.
Related
I try to create a class in matlab
It has a property children
properties
children
If this variable is written to - it is supposed to be an arrays of structs
it fails with
function obj = Init(obj, valueList)
%INIT Initialise with vector of new parameter sets
newSet = obj.ParamSet;
newSet.values = valueList;
obj.children(end + 1) = newSet; % <<< error
Error is :
Conversion to double from struct is not possible.
This is the struct that is used
methods(Static)
function paramset = ParamSet()
newset.('values') = [];
newset.('fitness') = 0;
paramset = newset;
end
end
The simple solution is to assign if it’s empty:
if isempty(obj.children)
obj.children = newSet;
else
obj.children(end + 1) = newSet;
end
I am trying to assign the field values of structure in loop.
Structure declaration with empty values:
result_struct = struct('a',{},'b',{},'c',{},'d',{})
I am assigning values in loop like that:
% assume a, b, c, d are variables in my workspace
% field names match with the variable names
for index=1:n
% some computation and store results in variables (a-d)
result_struct(index).a = a;
result_struct(index).b = b;
result_struct(index).c = c;
result_struct(index).d = d;
end
How can I assign the values to the fields using another loop? Like that:
for fname = fieldnames(result_struct)'
result_struct(index).fname = fname; % field names and variable names match
end
You need to use dynamic field names to assign to the struct (the leff-hand side). For the right hand side you could use eval but that is dangerous, so it's better to save your variable fname to a file and then load it back in as a struct prior to accessing fname, again using dynamic field names.
names = fieldnames(result_struct);
for k = 1:numel(names)
% Save variable to a file
save('tmp.mat', names{k});
% Load it back into a struct
tmp = load('tmp.mat', names{k});
result_struct(index).(names{k}) = tmp.(names{k});
end
Alternately, you can use the save and load to just transform the entire thing into a struct without having to loop through the fields.
fields = fieldnames(result_struct);
% Save all relevant variables to a file
save('tmp.mat', fields{:});
% Load it back into the result_struct
result_struct(index) = orderfields(load('tmp.mat'), fields);
I want to compare two strings from two structs. My Code is like below:
%matlab model scan
[variables] = Simulink.findVars('myModell');
variablesNames =[];
%save the names of all the variables in variablesNames
for t= 1:length(variables)
variablesNames(t).Name = variables(t).Name;
end
%scan workspace
for f = fieldnames(my_workspace)
found = false;
for c = 1:length(variablesNames)
if strcmp(f{c}, variablesNames(c))
found = true;
result = 'Found in Workspace: ';
end
if ~found
result = 'Not found inside Workspace';
end
end
disp(['Workspace Variable: ', sprintf('%-*s',40,f{c}), result]);
end
variablesNames is a struct 1x15 with 1 field
my_workspace is 1x1 struct with 20 fields
I got only one variable as return.
What is wrong in this code?
I don't really understand why are you creating a new struct here: variablesNames(t).Name, therefore I just removed that part.
The modified code just iterates through the variables struct-array and checks whether variable my_workspace has a field with the name of the value stored in the Name field of the currently processed element, using isfield.
[variables] = Simulink.findVars('myModell');
for i = 1:length(variables)
if isfield(my_workspace, variables(t).Name)
result = 'Found in Workspace: ';
else
result = 'Not found inside Workspace';
end
disp(['Workspace Variable: ', sprintf('%-*s', 40, variables(t).Name), result]);
end
I'm trying to generate a collection of information about a set of images, so I've created a struct array as follow,
resultsInfo = struct('img_index',0,'correlated',cell(1,5),'correlationFactor',zeros(1,5),'ImgSum',zeros(640,480));
Where: img_index is an integer that represents the image, the correlated is the cells containing the name of correlated images, correlationFactor is a number which represents how the images are similar, and the imgSum is a sum of correlated images.
I want to create the array in a dynamic way, inside a for loop, but the code generates only the fifth imageSums of each element.
How can I start the struct to fill all the elements of the array with the matrix of zeros?
Please define following method :
function resultStruct = CreateEmptySruct ()
resultStruct.img_index = 0 ;
resultStruct.correlated = cell(1,5);
resultStruct.correlationFactor = zeros(1,5);
resultStruct.ImgSum = zeros(640,480) ;
end
Then call this method in For loop like this:
for i = 1 :5
structArray(i) = CreateEmptySruct () ;
end
However, you can also set any value to each struct individually by passing function arguments.
I need to rename a bunch of fields in a structure by basically changing the prefix on it.
e.g.
MyStruct.your_firstfield
MyStruct.your_secondfield
MyStruct.your_thirdfield
MyStruct.your_forthfield
%etc....
to
MyStruct.my_firstfield
MyStruct.my_secondfield
MyStruct.my_thirdfield
MyStruct.my_forthfield
%etc...
without typing each one out...since there are many and may grow.
Thanks!
You can do this by dynamically generating field names for the output struct.
% Example input
MyStruct = struct('your_firstfield', 1, 'your_secondfield', 2, 'your_thirdfield', 3 );
% Get a cell array of MyStruct's field names
fields = fieldnames(MyStruct);
% Create an empty struct
temp = struct;
% Loop through and assign each field of new struct after doing string
% replacement. You may need more complicated (regexp) string replacement
% if field names are not simple prefixes
for ii = 1 : length(fields)
temp.(strrep(fields{ii}, 'your', 'my')) = MyStruct.(fields{ii});
end
% Replace original struct
MyStruct = temp;