Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a string to refer to a structure array - matlab
    primarykey
    data
    text
    <p>I am trying to take the averages of a pretty large set of data, so i have created a function to do exactly that. The data is stored in some <code>struct1.struct2.data(:,column)</code> there are 4 <code>struct1</code> and each of these have between 20 and 30 sub-<code>struct2</code> the data that I want to average is always stored in column 7 and I want to output the average of each <code>struct2.data(:,column)</code> into a <code>2xN</code> array/double (column 1 of this output is a reference to each sub-<code>struct2</code> column 2 is the average) </p> <p>The omly problem is, I can't find a way (lots and lots of reading) to point at each structure properly. I am using a string to refer to the structures, but I get error <code>Attempt to reference field of non-structure array.</code> So clearly it doesn't like this. Here is what I used. (excuse the inelegence)</p> <pre><code>function [avrg] = Takemean(prefix,numslits) % place holder arrays avs = []; slits = []; % iterate over the sub-struct (struct2) for currslit=1:numslits dataname = sprintf('%s_slit_%02d',prefix,currslit); % slap the average and slit ID on the end avs(end+1) = mean(prefix.dataname.data(:,7)); slits(end+1) = currslit; end % transpose the arrays avs = avs'; slits = slits'; avrg = cat(2,slits,avs); % slap them together </code></pre> <p>It falls over at this line <code>avs(end+1) = mean(prefix.dataname.data,7);</code> because as you can see, <code>prefix</code> and <code>dataname</code> are strings. So, after hunting around I tried making these strings variables with <code>genvarname()</code> still no luck!</p> <p>I have spent hours on what should have been 5min of coding. :'(</p> <p>Edit: Oh <code>prefix</code> is a string e.g. <code>'Hs'</code> and the structure of the structures (lol) is e.g. <code>Hs.Hs_slit_XX.data()</code> where <code>XX</code> is e.g. <code>01,02,...27</code></p> <p>Edit: If I just run <code>mean(Hs.Hs_slit_01.data(:,7))</code> it works fine... but then I cant iterate over all of the <code>_slit_XX</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload