MATLAB Answers

Mau
0

How to add an extra column to a 1x1 structure

Asked by Mau
on 6 Aug 2019
Latest activity Commented on by Mau
on 21 Aug 2019 at 20:36
Hi,
I am new to Matlab an my question may not faithfully reflect what I am trying to do. I have managed to process some data by trail and error and a lot of internet searching. I am sure that my solutions are not optimal, so feel free to comment on anything, all suggestions are welcome.
All my results are stored in a nested structure called 'Results'. The first 'level' (this is probably the wrong terminology) is a 1x1 struct with 4 fields (one for each of my 4 samples). The fields are in the rows and it has only 2 columns: 'Field' and 'Value'. Under 'Field' I have the names of my samples, under 'Value' I have non-scalar nested structures that hold my results. It took me along time to get here, but I am satisfied with this way to store the data (but any suggestions are welcome).
The first 'level' of 'Results' looks something like this:
samples = {'Forest', 'Tundra', 'Lake', 'Ocean'}
for i = 1:length(samples)
Results.(samples{i}) = struct();
end
Afterwards I wrote some long code that analyzes my data and stores the results for each sample in nested structures under 'Value'. That is all good.
What I particularly like about 'Results' is that I can index by sample (e.g. Results.Ocean). Due to the way my raw data is stored in .mat files, I find this more thrustworthy than using row number.
What I failed to do was to add a third column to this first 'level' of 'Results'. I want this column to be called 'warnings', and be able to write 'yes' in the row for the sample 'Lake', and the rest of the samples would display [ ]. I managed to add said column to the nested structures ('levels') within a sample, by:
Results.Ocean(5).warnings = 'yes'
I realize this is a bit confusing (just imagine how I feel!). I am happy to provide more info is required.
Thanks!
Mau

  2 Comments

I don't think you can add "3rd column" to your structure because that will cause structure misbehave. For e.g. Let's assume you created a Structure with 'Field' 'Value' and 'Warning'. Now, when you call any field what will it return? It cannot written both because that will mean you are assigning two values to one variable. So simply put, Structure is only made up of fields and it's corresponding values.
However, you can achieve similar result to what you want. One of the method is, add a third layer to the structure in between current 1st and 2nd, which will store "Value" and "Warnings" to the corresponding fireld. So, modified code will look like this:
samples = {'Forest', 'Tundra', 'Lake', 'Ocean'}
for i = 1:length(samples)
Results.(samples{i}).Value = struct();
Results.(samples{i}).Warning = [];
if strcmp(samples{i},'Lake')
Results.(samples{i}).Warning = 'yes';
end
end
I hope it helps !
Note: you will have to update "Results.sample{i}.Value" instead of "Results.sample{i}" accordingly
Thank you for your reply. I understand your suggestion, but I specially value your explanation about the structure format (matlab documentation is surprisingly and regrettably superficial, in my opinion as a beginner). I am wondering if maybe a structure was the wrong choice to store my data, or at least the first layer of it.
Please do make suggestions or point me to relevant reading material or tutorials.
Thank you!

Sign in to comment.

Tags

Products


Release

R2019a

1 Answer

Answer by Matt J
on 9 Aug 2019
Edited by Matt J
on 9 Aug 2019
 Accepted Answer

I can't be sure from your description exactly what you are trying to do, but it sounds like a table might be much better for what you are pursuing than a struct. A table, unlike a 1x1 struct, actually does have rows and columns, which you can name and manipulate, e.g.,
>> samples = {'Forest', 'Tundra', 'Lake', 'Ocean'};
>> ColumnNames={'Value','Warning'};
>> T=table('Size',[4,2],'VariableTypes',{'double','string'}, 'RowNames',RowNames,'VariableNames',ColumnNames);
>> T{'Ocean','Warning'}="yes"
T =
4×2 table
Value Warning
_____ _________
Forest 0 <missing>
Tundra 0 <missing>
Lake 0 <missing>
Ocean 0 "yes"

  1 Comment

Mau
on 21 Aug 2019 at 20:36
Hi Matt,
Thank you for your suggestion. A table solved my problem by allowing the extra column. The trade-off was that it wasn't straightforward to store my structure arrays in a column variable ("Value", in your example). I think the reason for this is that my structure arrays have different sizes.
I circumvented this by storing the structure arrays in cel arrays in the "Value" column. I got the tip here: https://www.mathworks.com/matlabcentral/answers/309889-how-does-one-save-a-struct-type-to-an-individual-cell-inside-a-table
This seems unnecessarily complicated and I wonder what future difficulties I am creating for myself, but it will do for now. Thank you

Sign in to comment.