What is the difference between curly braces and paranthesis - matlab

I have a couple of line of code which compares some values in two different matrices and even if it is true it doesn't enter the if part.
for i = 1:ux
for j = 1:SIR
if ShelfInfo{SIR, 2} == uniquexy(ux, 1) && uniquexy{ux, 2} == ShelfInfo{SIR, 3}
shelf = ShelfInfo{j,5};
shelves = [shelves; shelf];
1
end
end
end
This code is work but it doesn't enter the if part. I believe it is because of the braces. When I changed everything with curly braces I am receiving this error Brace indexing is not supported for variables of this type. When I am changing this braces with parentheses I am receiving this error Undefined operator '==' for input arguments of type 'table'.
I can't find what to do can you help me with it?

()-indexing subsets an array by elements, and works on any type of array.
{}-indexing subsets a cell array, and extracts the cells' contained values. Basically, it "reaches into" the cells and pulls out their contents. It only works on cell arrays, or objects that have overloaded subsref() to provide this behavior.
I'm guessing you're accidentally applying {}-indexing to your uniquexy in one of your references there, when both of them should be ()-indexing:
... uniquexy(ux, 1) && uniquexy{ux, 2} ...

Besides the indexing problem (which depends on the data type of your matrices and would be handy to give as a part of a minimal working example), in your if-statement you do not loop over your array elements. I assume you would want to use indices i and j, instead of SIR and ux (they indicate a fixed position in your arrays). So why do you need the if-statement inside two for-loops then?
May be check these links on accessing array elements depending on array types:
Basic array indexing
Cell vs. structure arrays
Tables

Related

How to make A == B call ismember(A, B) in Matlab when A and B are cell arrays

Here is some data.
A = {'asdf' 'qwer'};
When I use ==, I get
>> A == 'asdf'
Undefined operator '==' for input arguments of type 'cell'.
which is expected. A workaround is to do
>> ismember(A, 'asdf')
ans =
1 0
How do you add a method for the cell class so that == defaults to calling ismember in the above fashion?
You can overload #eq for cell arrays by following the explanation in this answer.
Why is that a bad idea? ismember only works if there are strings inside the cell array, but not other data types. Also, it will make your code not portable, as for example I would not want to use code that overloads operators for built-in data types.
If you really want to have a cell-array-like data type, where you can use #eq as ismember, you can create your custom class myCellArray that subclasses cell and that guarantees that each element contains a string. Then, you can go and overload operators all you want.
If you want to test whether the string 'asdf' is in the cell array {'asdf','qwer'}, you should probably use the ismember function:
my_set = {'asdf','qwer'};
logical_result = ismember('asdf',my_set);
There are major stylistic problems with doing what you are suggesting:
Defining the operator == in such a way that it isn't reflexive (i.e. A==A returns false) and isn't symmetric (A==B isn't the same as B==A) is bizarre.
Your code won't be portable as it will require people hacking their def. of cell array to work. Almost no one will be willing to do this.
Changing the code for cell array has the potential to introduce confusing errors down the line.
Alternatively, putting in the time and effort into writing your own cell array wrapper/replacement class is possible, but would be a horrible waste of time and needlessly slow.

Nested Cell Structure in MATLAB

I am attempting to organize a large amount of data into nested structures in MATLAB and I would like for each structure to contain a cell array but I get
A dot name structure assignment is illegal when the structure is empty. Use a subscript on the structure.
Example of code:
Year.Org1 = struct('Set1',{},'Set2',{});
Year.Org2 = struct('Set1',{},'Set2',{});
and then I want Set1/Set2/etc to be cell arrays of n-rows with column 1 str, column 2 str, column 3 value, and so on.
Any advice on initializing this structure and then accessing various parts would be greatly appreciated.
With single curly braces the structure is initialized empty. You can achieve what you want by doubling the curly braces:
Year.Org1 = struct('Set1',{{}},'Set2',{{}});
Best,

MATLAB - How to solve error, "Scalar index required for this type of multi-level indexing"?

I have a struct called "Trials" and a field called "peakvel" which is inside a struct called "sac1str", which is then inside Trials.
I want to extract the data inside the field "peakvel".
This is my code:
ed(fileidx).vel_up = [Trials(Lup & Lgoodtrials).sac1str.sac1ovr_peakvel];
Whenever I run this I get an error, "Scalar index required for this type of multi-level indexing."
Any ideas to get around this error?
The chaining of non-scalar indexing ((Lup & Lgoodtrials)) and accesing two levels of structs is not possible. Use a temporary variable in between:
temp=[Trials(Lup & Lgoodtrials)].sac1str
ed(fileidx).vel_up=[temp.sac1ovr_peakvel]
There are three types of subsref, first two are the indexing operations for cells and arrays, third is the access to a field of a struct. When using non-scalar indices (more than one element is indexed) on a top-level, you can access only one further level of the struct. The error says you can not combine multi-level indexing (in the struct) with non scalar indices (for the array). Not allowed is:
Trials(Lup & Lgoodtrials).sac1str.sac1ovr_peakvel
But allowed is the 1-level indexing:
Trials(Lup & Lgoodtrials).sac1str
Or use of scalar idices:
s=2
Trials(s).sac1str.sac1ovr_peakvel

Can the "s{1} annoyance" when iterating over a cell array be avoided?

The "s{1} annoyance" of the title refers to the first line within the for-block below:
for s = some_cell_array
s = s{1}; % unpeel the enclosing cell
% do stuff with s
end
This s = s{1} business is necessary because the iteration over some_cell_array does not really iterate over the contents of some_cell_array, but rather over 1-element cells, each containing an item from some_cell_array.
Putting aside the question of who could possibly want this behavior as the default, is there any way to iterate over the bare contents of some_cell_array?
I don't think there is a way to avoid this problem in the general case. But there is a way if your cell array has all numbers or all chars. You can convert to an array and let the for loop iterate over that.
For example, this:
some_cell_array = {1,2,3}
for s = [some_cell_array{:}] % convert to array
s
end
Gives:
s =
1
s =
2
s =
3
Another option is to create a function that operates on every cell of the array. Then you can simply call cellfun and not have a loop at all.
I don't have any ideas about who would want this behavior or how it could be useful. My guess as to why it works this way, however, is that it's an implementation thing. This way the loop iterator doesn't change type on different iterations. It is a cell every time, even if the contents of that cell are different types.
Just a small add-on to Sam Robert's comment to the original question, on why you should prefer s{:} over s{1} : easier bug tracking.
Imagine you mistakenly stored your cell s as a column instead of a line. Then
for s = some_cell_array
will simply return a cell s which is equal to some_cell_array. Then the syntax s{1} will return the first element of some_cell_array, whereas s{:} will produce a list of all elements in some_cell_array. This second case will much more surely lead to an execution error in the following code. Whereas the first case could sometimes create a hard bug to detect.

Avoiding eval in assigning data to struct array

I have a struct array called AnalysisResults, that may contain any MATLAB datatypes, including other struct arrays and cell arrays.
Then I have a string called IndexString, which is the index to a specific subfield of StructArray, and it may contain several indices to different struct arrays and cell arrays, for example:
'SubjectData(5).fmriSessions{2}.Stats' or 'SubjectData(14).TestResults.Test1.Factor{4}.Subfactor{3}'.
And then I have a variable called DataToBeEntered, which can be of any MATLAB datatype, usually some kind of struct array, cell array or matrix.
Using eval, it is easy to enter the data to the field or cell indexed by IndexString:
eval([ 'AnalysisResults.', IndexString, ' = DataToBeEntered;' ])
But is it possible to avoid using eval in this? setfield doesn't work for this.
Thank you :)
Well, eval surely is the easiest way, but also the dirtiest.
The "right" way to do so, I guess, would be to use subsasgn. You will have to parse the partial MATLAB command (e.g. SubjectData(5).fmriSessions{2}.Stats) into the proper representation for those functions. Part of the work can be done by substruct, but that is the lightest part.
So for example, SubjectData(5).fmriSessions{2}.Stats would need to be translated into
indexes = {'.' , 'SubjectData',
'()', {5},
'.' , 'fmriSessions',
'{}', {2},
'.' , 'Stats'};
indexStruct = substruct(indexes{:});
AnalysisResult = subsasgn(AnalysisResult, indexStruct, DataToBeEntered);
Where you have to develop the code such that the cell array indexes is made as above. It shouldn't be that hard, but it isn't trivial either. Last year I ported some eval-heavy code with similar purpose and it seemed easy, but it is quite hard to get everything exactly right.
You can use dynamic field names:
someStruct.(someField) = DataToBeEntered;
where someField is a variable holding the field name, but you will have to parse your IndexString to single field name and indices.