Comments on: Working with Arrays of Structures https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Wed, 18 Jan 2012 22:29:57 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Loren Shure https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-32920 Wed, 18 Jan 2012 22:29:57 +0000 https://blogs.mathworks.com/loren/?p=67#comment-32920 Darin-

I am not sure. I would recommend contacting support and reporting the behavior as a bug, which it seems like to me. But if I heard a reason that made sense, I might be swayed the other way.

–Loren

]]>
By: Darin https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-32917 Wed, 18 Jan 2012 19:17:32 +0000 https://blogs.mathworks.com/loren/?p=67#comment-32917 Hi Loren,

I get you on how struct array references like, temp(3:5).A, return coma lists… and, like you, don’t get the various confusions this seems to cause.

But I’m trying to understand an inconsistency (BUG?) in this behavior:

[temp(false)] = nan or [temp(4:3)] = nan

correctly, does nothing when temp is a regular array, BUT when temp is a structure containing a field “A”:

[temp(false).A] = nan or [temp(4:3).A] = nan

(Which looks look me like an IDENTICAL case given, the comma list interpretation)

gives an error about “too many elements” on the right side.

Is there a rational reason for this inconsistency that you can help me follow… or is it a BUG? It certainly complicates code to special-handle the null case.

Or, am I missing a shortcut there?

Thanks,
Darin

]]>
By: Loren https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-32688 Fri, 18 Nov 2011 13:53:22 +0000 https://blogs.mathworks.com/loren/?p=67#comment-32688 Anna-

What code have you tried?

–Loren

]]>
By: Anna https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-32684 Fri, 18 Nov 2011 12:21:24 +0000 https://blogs.mathworks.com/loren/?p=67#comment-32684 Sorry here is the code…..

A = [1 2 3; 1 2 4]

f(1).h = [1 2 5; 2 5 6; 1 2 3; 3 5 6]
f(2).h = [1 2 6; 3 5 6; 1 5 3; 3 5 6]
f(3).h = [1 2 4; 3 4 5; 1 1 3; 3 4 6]

[m,n] = arrayfun(@(x)find(x.h==A),f,'uniformoutput',false)
]]>
By: Anna https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-32683 Fri, 18 Nov 2011 12:17:32 +0000 https://blogs.mathworks.com/loren/?p=67#comment-32683 Hi Loren,

Can you tell me how to use the arrayfun to locate values that match entire rows in data.

i.e.

If I do this, then I get….
??? Error using ==> eq
Matrix dimensions must agree.

Error in ==> @(x)find(x.h==A)

Thanks,

Anna

]]>
By: Loren https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-32559 Fri, 21 Oct 2011 13:28:18 +0000 https://blogs.mathworks.com/loren/?p=67#comment-32559 HH,

Thanks for sharing your thoughts. I have made sure they got into the bug/enhancement list.

–Loren

]]>
By: HH https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-32557 Thu, 20 Oct 2011 13:51:50 +0000 https://blogs.mathworks.com/loren/?p=67#comment-32557 Loren,

struct arrays seem very unwieldy, they are hard to create via preallocation, and seem impossible to merge without looping, e.g. if you have a struct array O where O(i) is combination of the two structs a and b, then there is little hope of creating O(i) = mergestruct(a,b) (substitute for mergestruct your favourite vertcat function for structs here).

It would be great if mathworks provided some more tools for struct arrays, a separate class name, detection via is* functions, and vertcat methods, easier syntax for creating (preallocation especially), etc.

Best, HH

]]>
By: Loren https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-31743 Mon, 11 Oct 2010 18:34:22 +0000 https://blogs.mathworks.com/loren/?p=67#comment-31743 Miguel-

How large is f? I wonder if the code would be clearer if you wrote the for-loop.

One way is to convert the struct to a cell and work on each cell. Then convert back to a struct. Not sure this is worth the effort however.

Others?
–loren

]]>
By: Miguel https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-31742 Mon, 11 Oct 2010 18:09:56 +0000 https://blogs.mathworks.com/loren/?p=67#comment-31742 Hi Loren

I am beginning to learn Matlab and I have a question related to this post. Using your example, if i wanted to, first, find certain values in a struct and then replace them by another value, say 0. So the first thing would be like this:

f(1).h = [1 2 3 4];
f(2).h = [5 6 3 8];
[m,n] = arrayfun(@(x)find(x.h==3),f,'uniformoutput',false)

Now that i have the indexes, how can I replace those “3” by, for example, 0? I mean without using loops. Thanks a lot in advance.

]]>
By: Loren https://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-31560 Wed, 04 Aug 2010 13:55:19 +0000 https://blogs.mathworks.com/loren/?p=67#comment-31560 Emil-

struct2cell should get you part of the way there – but not the data not arranged as a struct per se. You can use a for loop. Or cellfun (which effectively has a loop inside it).

–Loren

]]>