I am currently trying to map some data within a month depending on 'Month', 'week', 'day'.
This means that in principle I have three keys. I thought of following solutions to the problem:
1)
containers.Map(month,containers.Map(week,containers.Map(day,value)))
The problem with this solutions is to reference it I must use: How can I index a MATLAB array returned by a function without first assigning it to a local variable?.
And I find this solution rather "ugly".
2)
So another way would be to use a Map with multiple keys. The containers documentation show that only a single dimension key is allowed.
Question:
Do you have any trick to solve this multiple key problem?
Update/Solution:
I ended using a concatenated string as a key (as suggested) I added following piece of code to make the containers.Map as a proper multi-dimensional hashmap. Take a look below (I excluded week for simplification):
classdef example
properties
myMap % Map for storage
end
methods
function obj = example()
obj.myMap = containers.Map;
end
function obj2 = setVal(obj2,value,Month,DayType)
key = strcat(num2str(Month),'-',num2str(DayType));
obj2.myMap(key) = value;
end
function value = getValue(obj,Month,DayType)
key = strcat(num2str(Month),'-',num2str(DayType));
value = obj.myMap(key);
end
end
You could perhaps use all three of those keys to build a single key. I'm assuming this can be done as Month, week and day could be considered as unique. There is only one unique combination of these per occurrence. As such, simply take these keys and build them into single strings, then use theses as keys into your dictionary / containers.Map().
Here's an example:
%// Test data
month1 = 'May';
week1 = 2;
day1 = 'Thursday';
month2 = 'June';
week2 = 3;
day2 = 'Friday';
month3 = 'July';
week3 = 4;
day3 = 'Sunday';
%// Define keys
key1 = [month1 num2str(week1) day1];
key2 = [month2 num2str(week2) day2];
key3 = [month3 num2str(week3) day3];
%// Build dictionary
M = containers.Map();
M(key1) = 'Hello!';
M(key2) = 'Testing!';
M(key3) = 'Yes!';
%// Now test accessing
disp(M(key1));
disp(M(key2));
disp(M(key3));
The above code will take three months, three weeks and days, convert them into strings, and use these as keys into your dictionary. I don't know what the output type is for your purposes, so I just assigned strings. Note that I took the numbers and used num2str to convert the numbers into strings to ensure compatibility with the rest of the string. I don't know what data type week is (or any of the other variables in fact...), so simply use what I have and modify it for your own purposes.
I create the dictionary, then to test it, I access each of the values with each of the keys. As expected, my output is:
Hello!
Testing!
Yes!
There is a new MapNested class on Matlabs File-exchange:
http://de.mathworks.com/matlabcentral/fileexchange/62492-mapnested-implementation-for-nested-maps--map-of-maps-
or on github:
https://github.com/RolandRitt/Matlab-NestedMap
Check it out!
The syntax for setting an value is the following:
NMapobj = MapNested(); %constructor;
NMapobj(key1, key2, key3) = value;
for retrieving:
value = NMapobj(key1, key2, key3);
Related
I have a long list of variables in my workspace.
First, I'm finding the potential variables I could be interested in using the who function. Next, I'd like to loop through this list to find the size of each variable, however who outputs only the name of the variables as a string.
How could I use this list to refer to the values of the variables, rather than just the name?
Thank you,
list = who('*time*')
list =
'time'
'time_1'
'time_2'
for i = 1:size(list,1);
len(i,1) = length(list(i))
end
len =
1
1
1
If you want details about the variables, you can use whos instead which will return a struct that contains (among other things) the dimensions (size) and storage size (bytes).
As far as getting the value, you could use eval but this is not recommended and you should instead consider using cell arrays or structs with dynamic field names rather than dynamic variable names.
S = whos('*time*');
for k = 1:numel(S)
disp(S(k).name)
disp(S(k).bytes)
disp(S(k).size)
% The number of elements
len(k) = prod(S(k).size);
% You CAN get the value this way (not recommended)
value = eval(S(k).name);
end
#Suever nicely explained the straightforward way to get this information. As I noted in a comment, I suggest that you take a step back, and don't generate those dynamically named variables to begin with.
You can access structs dynamically, without having to resort to the slow and unsafe eval:
timestruc.field = time;
timestruc.('field1') = time_1;
fname = 'field2';
timestruc.(fname) = time_2;
The above three assignments are all valid for a struct, and so you can address the fields of a single data struct by generating the field strings dynamically. The only constraint is that field names have to be valid variable names, so the first character of the field has to be a letter.
But here's a quick way out of the trap you got yourself into: save your workspace (well, the relevant part) in a .mat file, and read it back in. You can do this in a way that will give you a struct with fields that are exactly your variable names:
time = 1;
time_1 = 2;
time_2 = rand(4);
save('tmp.mat','time*'); % or just save('tmp.mat')
S = load('tmp.mat');
afterwards S will be a struct, each field will correspond to a variable you saved into 'tmp.mat':
>> S
S =
time: 1
time_1: 2
time_2: [4x4 double]
An example writing variables from workspace to csv files:
clear;
% Writing variables of myfile.mat to csv files
load('myfile.mat');
allvars = who;
for i=1:length(allvars)
varname = strjoin(allvars(i));
evalstr = strcat('csvwrite(', char(39), varname, '.csv', char(39), ', ', varname, ')');
eval(evalstr);
end
I have a cell array that contains words like 'a', 'b', and 'c' for example. What I want to be able to do is use Matlab's containers.Map to make a hash table that I can add values to for each key and be able to look them up quickly. I am able to do this if I do not initialize my containers.Map object beforehand as follows but it does not allow me (or at least I haven't found a way) to add more key/value pairs later and makes it so that I have to reinitialize the object during each iteration of a loop:
key = {'a','b','c'};
newmap = containers.Map(key,[1,2,3]);
My problem is that I need to be able to continually add new keys to the hash table and therefore cannot keep initializing the containers.Map object each time, I want one long hash table with all the keys and values that I get while in a loop.
Here is the code that I am trying to get working, I want to be able to add the keys to the containers.Map object newmap and their corresponding values at the same time. The keys are always strings in a cell array and the values are always integers:
key = {'a','b','c'};
val = [1,2,3];
newmap = containers.Map(); % containers.Map object initialization
newmap(key) = val;
My desired output would something like this:
newmap(key)
ans = 1 2 3
Attempts at solving this:
I have tried converting the cell array of keys using cellstr() and char() but haven't had any luck with these. I seem to keep getting this error when trying this:
Error using containers.Map/subsref
Specified key type does not match the type expected for this container.
Thanks for any help.
% Initialize
map = containers.Map('KeyType','char','ValueType','double');
% Assume you get these incrementally
key = {'a','b','c'};
val = [1,2,3];
% Add incrementally
for ii = 1:numel(key)
map(key{ii}) = val(ii);
end
You can retrieve all values at once for a given set of keys, but you will get a cell array. No way around it, but you can convert with cell2mat(), or retrieve incrementally with a loop map(key{ii})
% Retrieve all
values(map,key)
ans =
[1] [2] [3]
I am trying to assign valuable, which is number and given by for loop, to the name of structure field. For example, I would like to do as following,
A.bx, where A is name of structure(= char), b is part of field name ( = char) and x is valuable given by for loop. A and b is fixed or predefined.
Any comment is appreciated !
genvarname(str,list) generates a valid variable name in str [a string] in which at each iteration value in str is different from the exclusion list
And fieldname(S) returns a list of all the names of the field already in the structure S (use it to create a exclusion list)
Here is a code for what you want:
A = struct ();
for i = 1:5
A.(genvarname ('b', fieldnames (A))) = i;
end
Read about 1. genvarname(str,list) 2. fieldnames(S)
You can name you struct fields using simple sprintf
A = struct()
for ii = 1:10
fn = sprintf('b%d', ii );
A.(fn) = ii; % use the struct
end
I tend to agree with sebastian that suggested using arrays or cells over this type of field naming. In addition to cells and arrays you might find containers.Map to be very versatile and useful.
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 struct1.struct2.data(:,column)
there are 4 struct1 and each of these have between 20 and 30 sub-struct2
the data that I want to average is always stored in column 7 and I want to output the average of each struct2.data(:,column) into a 2xN array/double (column 1 of this output is a reference to each sub-struct2 column 2 is the average)
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 Attempt to reference field of non-structure array. So clearly it doesn't like this. Here is what I used. (excuse the inelegence)
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
It falls over at this line avs(end+1) = mean(prefix.dataname.data,7); because as you can see, prefix and dataname are strings. So, after hunting around I tried making these strings variables with genvarname() still no luck!
I have spent hours on what should have been 5min of coding. :'(
Edit: Oh prefix is a string e.g. 'Hs' and the structure of the structures (lol) is e.g. Hs.Hs_slit_XX.data() where XX is e.g. 01,02,...27
Edit: If I just run mean(Hs.Hs_slit_01.data(:,7)) it works fine... but then I cant iterate over all of the _slit_XX
If you simply want to iterate over the fields with the name pattern <something>_slit_<something>, you need neither the prefix string nor numslits for this. Pass the actual structure to your function, extract the desired fields and then itereate them:
function avrg = Takemean(s)
%// Extract only the "_slit_" fields
names = fieldnames(s);
names = names(~cellfun('isempty', strfind(names, '_slit_')));
%// Iterate over fields and calculate means
avrg = zeros(numel(names), 2);
for k = 1:numel(names)
avrg(k, :) = [k, mean(s.(names{k}).data(:, 7))];
end
This method uses dynamic field referencing to access fields in structs using strings.
First of all, think twice before you use string construction to access variables.
If you really really need it, here is how it can be used:
a.b=123;
s1 = 'a';
s2 = 'b';
eval([s1 '.' s2])
In your case probably something like:
Hs.Hs_slit_01.data= rand(3,7);
avs = [];
dataname = 'Hs_slit_01';
prefix = 'Hs';
eval(['avs(end+1) = mean(' prefix '.' dataname '.data(:,7))'])
I have a matlab structure that follows the following pattern:
S.field1.data1
...
.field1.dataN
...
.fieldM.data1
...
.fieldM.dataN
I would like to assign values to one data field (say, data3) from all fields simultaneously. That would be semantically similar to:
S.*.data3 = value
Where the wildcard "*" represents all fields (field1,...,fieldM) in the structure. Is this something that can be done without a loop in matlab?
Since field1 .. fieldM are structure arrays with identical fields, why not make a struct array for "field"? Then you can easily set all "data" members to a specific value using deal.
field(1).data1 = 1;
field(1).data2 = 2;
field(2).data1 = 3;
field(2).data2 = 4;
[field.data1] = deal(5);
disp([field.data1]);
A loop-based solution can be flexible and easily readable:
names = strtrim(cellstr( num2str((1:5)','field%d') )); %'# field1,field2,...
values = num2cell(1:5); %# any values you want
S = struct();
for i=1:numel(names)
S.(names{i}).data3 = values{i};
end
In simple cases, you could do that by converting your struct into a cell array using struct2cell(). As you have a nested structure, I don't think that will work here.
On the other side, is there any reason why your data is structured like this. Your description gives the impression that a simple MxN array or cell array would be more suitable.