Dynamic switch in Matlab MAT file - matlab

I have a Matlab mat file, which has the following variables:
variable0
variable1
variable2
variable3
Is it possible to dynamically index them and modify, something like this:
function setVariable(obj, variableNum, data)
obj.matFile.(variable0+variableNum) = data;
end
So, if someone passes 0 variable 0 is modified and if someone passes 3 then variable three. I know this code doesn't work, this is just some example of what I tried. My current solution is to use a switch statement. This is not so good as in the C++ code, I am using indexing like above. I would like the C++ and Matlab to be as close as possible.
ANSWER
I did it this way and it is working:
eval(sprintf('obj.matfile_variable%d = data;', variableNum));

function setVariable(obj, variableNum, data)
% check if variableNum is numeric
if isnumeric(variableNum)
variableNum = num2str(variableNum);
varName = strcat('variable',variableNum);
else
varName = strcat('variable',variableNum);
end
obj.matFile.(varName) = data;
end
This should do the trick.

What about using
obj.matFile(variableNum).data = data;

Related

Dynamically check for existence of structure field name with hierarchy

As a follow-up to my previous question about how to assign fields to a structure variable with a dynamic hierarchy, I would now like to be able to query those fields with isfield. However, isfield will only take one argument, not a list as with setfield.
To summarize my problem:
I have a function that organizes data into a structure variable. Depending on certain flags, the data is saved into the substructures with a different number of levels.
For instance, the accepted answer to my previous question has me doing this to build my structure:
foo = struct();
% Pick one...
true_false_statement = true;
% true_false_statement = false;
if true_false_statement
extra_level = {};
else
extra_level = {'baz'};
end
foo = setfield(foo, extra_level{:}, 'bar1', 1);
which gives me foo.bar1 = 1 if true_false_statement is true, and foo.baz.bar1 = 1 otherwise.
Now I want to test for the existence of the field (for instance to pre-allocate an array). If I do this:
if ~isfield(foo, extra_levels{:}, 'bar1')
foo = setfield(foo, extra_level{:}, 'bar1', zeros(1,100));
end
I get an error because isfield will only accept two arguments.
The best I've been able to come up with is to write a separate function with a try...catch block.
function tf = isfield_dyn(structure_variable, intervening_levels, field)
try
getfield(structure_variable, intervening_levels{:}, field);
tf = true;
catch err
if strcmpi(err.identifier, 'MATLAB:nonExistentField')
tf = false;
else
rethrow(err);
end
end
As mentioned below in the comments, this is a hacky hack way to do this, and it doesn't even work all that well.
Is there a more elegant built-in way to do this, or some other more robust way to write a custom function to do this?
You might find the private utility functions getsubfield, setsubfield, rmsubfield, and issubfield from the FieldTrip toolbox very handy. From the documentation of getsubfield:
% GETSUBFIELD returns a field from a structure just like the standard
% GETFIELD function, except that you can also specify nested fields
% using a '.' in the fieldname. The nesting can be arbitrary deep.
%
% Use as
% f = getsubfield(s, 'fieldname')
% or as
% f = getsubfield(s, 'fieldname.subfieldname')
%
% See also GETFIELD, ISSUBFIELD, SETSUBFIELD
I am somewhat confused because
isfield(foo, 'bar1')
isfield(foo, 'baz')
seem to work just fine on your example struct.
Of course, if you want to test more fields, just write a loop over those fieldnames and test them one by one. That may not look vectorized, but is definitely better than abusing a try-catch block to guide your flow.

1-line try/catch equivalent in MATLAB

I have a situation in MATLAB where I want to try to assign a struct field into a new variable, like this:
swimming = fish.carp;
BUT the field carp may or may not be defined. Is there a way to specify a default value in case carp is not a valid field? For example, in Perl I would write
my $swimming = $fish{carp} or my $swimming = 0;
where 0 is the default value and or specifies the action to be performed if the assignment fails. Seems like something similar should exist in MATLAB, but I can't seem to find any documentation of it. For the sake of code readability I'd rather not use an if statement or a try/catch block, if I can help it.
You can make your own function to handle this and keep the code rather clear. Something like:
swimming = get_struct(fish, 'carp', 0);
with
function v = get_struct(s, f, d)
if isfield(s, f)
v = s.(f); % Struct value
else
v = d; % Default value
end
Best,
From what I know, you can't do it in one line in MATLAB. MATLAB logical constructs require explicit if/else statements and can't do it in one line... like in Perl or Python.
What you can do is check to see if the fish structure contains the carp field. If it isn't, then you can set the default value to be 0.
Use isfield to help you do that. Therefore:
if isfield(fish, 'carp')
swimming = fish.carp;
else
swimming = 0;
end
Also, as what Ratbert said, you can put it into one line with commas... but again, you still need that if/else construct:
if isfield(fish,'carp'), swimming = fish.carp; else, swimming = 0;
Another possible workaround is to declare a custom function yourself that takes in a structure and a field, and allow it to return the value at the field, or 0.
function [out] = get_field(S, field)
if isfield(S, field)
out = S.(field);
else
out = 0;
end
Then, you can do this:
swimming = get_field(fish, 'carp');
swimming will either by 0, or fish.carp. This way, it doesn't sacrifice code readability, but you'll need to create a custom function to do what you want.
If you don't like to define a custom function in a separate function file - which is certainly a good option - you can define two anonymous functions at the beginning of your script instead.
helper = {#(s,f) 0, #(s,f) s.(f)}
getfieldOrDefault = #(s,f) helper{ isfield(s,f) + 1 }(s,f)
With the definition
fish.carp = 42
and the function calls
a = getfieldOrDefault(fish,'carp')
b = getfieldOrDefault(fish,'codfish')
you get for the first one
a = 42
and the previous defined default value for the second case
b = 0

Iterate through a lot of Structs

my Problem is the following:
I have about 300 Struct files given.
They are set up like this:
DSC_0001 has about 250 other struct files in it:
-> like this: DSC_0001.marker_1
And this one has 10 Numbers in it.
Like that:
DSC_0001.marker_1.flow_angle = 90
and now I want to iterate through all the Struct files
Something like that:
for i = 1:300
for j = 1:250
flow_angle = DSC_**i**.marker_**j**
end
end
Is there a way to do this?
I have the feeling that it could be really easy but I just can't find the solution...
I hope my question is clear enough...
Thanks for your help!
If possible don't use eval.
It depends on how your data is stored, but one possiblity is that it is in a .mat file. In that case it can be loaded using
DSC_structs = load('My_DSC_struct_file.mat');
and then you can access the values like so:
for i = 1:300
for j = 1:250
flow_angle(i,j) = DSC_structs.(['DSC_' sprintf('%04d',i)]).(['marker_' sprintf('%d',j)]);
end
end
Why avoid the eval function
Edit: You say that each struct is in a different file. That's a bit messier. I would probably do something like this to load them:
DSC_structs = cell(1,300);
for i = 1:300
%Note: I'm guess at your file names here
DSC_structs{i} = load(['DSC_' sprintf('%04d',i) '.mat'];
end
and then access the values as
DSC_structs{i}.(['DSC_' sprintf('%04d',i)]).(['marker_' sprintf('%d',j)]);
I guess this is a use case for the dreaded eval function:
for i = 1:300
for j = 1:250
eval (['flow_angle = DSC_', sprintf('%04d',i), '.marker_', num2str(j)]);
end
end
BUT NB there are 2 problems with my code above
You haven't told us where you want to store your angle, so my code doesn't :/ but you'd want something like this if you just want to store them in a matrix: eval (['flow_angle(', num2str(i), ',', num2str(j), ') = DSC_', sprintf('%04d',i), '.marker_', num2str(j)])
eval is a horrible way of doing things but you're forced to because someone saved your data in a horrible. Sort yourself out now for the future by re-saving your data in a smarter way! so something like:
.
for i = 1:300
eval ( ['DSC(', num2str(i), ') = DSC_', sprintf('%04d',i)]);
end
%// then save DCS!
And now your can iterate through this matrix of structs rather than having a 300 structs polluting your workspace and forcing you to use eval

Matlab function switching return/no return

In Matlab, I've got a function that is called after some special points are found on an image. Depending on how the nearby pixels of that "special point" are, the function must return a structure with many parameters or return nothing.
Is it possible to have a function that by deafult will return something but, in some cases, nothing should be returned? How that "return nothing"'s code should be like? Thank you.
One common trick in matlab is to use the empty matrix [] to indicate nothing. You could write your function something like (untested code):
function result = analyze(image, special_point)
% your code here
if pixels_are_ok
result.a = 1;
result.b = 2;
else
result = [];
end
If you call this function from your other code, you can use isempty to see if you got a result or not:
result = analyze(image, special_point)
if isempty(result)
display('did not find anything')
else
display('found some interesting results')
display(result)
end

creating variables from structures in matlab

I have the following example which expresses the type of problem that I'm trying to solve:
clear all
textdata = {'DateTime','St','uSt','Ln','W'};
data = rand(365,4);
Final = struct('data',data,'textdata',{textdata})
clear textdata data
From this, Final.data contains values which correspond to the headings in Final.textdata excluding the first ('DateTime') thus Final.data(:,1) corresponds to the heading 'St'... and so on. What I'm trying to do is to create a variable in the workspace for each of these vectors. So, I would have a variable for St, uSt, Ln, and W in the workspace with the corresponding values given in Final.data.
How could this be done?
Will this solve your problem:
for ii=2:length( textdata )
assignin('base',Final.textdata{ii},Final.data(:,ii-1));
end
Let me know if I misunderstood.
The direct answer to your question is to use the assignin function, like so (edit: just like macduff suggested 10 minutes ago):
%Starting with a Final structure containing the data, like this
Final.textdata = {'DateTime','St','uSt','Ln','W'};
Final.data = rand(365,4);
for ix = 1:4
assignin('base',Final.textdata{ix+1}, Final.data(:,ix));
end
However, I strongly discourage using dynamic variable names to encode data like this. Code that starts this way usually ends up as spaghetti code full of long string concatenations and eval statements. Better is to use a structure, like this
for ix = 1:4
dataValues(Final.textdata{ix+1}) = Final.data(:,ix);
end
Or, to get the same result in a single line:
dataValues = cell2struct(num2cell(Final.data,1), Final.textdata(2:end),2)