Using data from a cell
8 views (last 30 days)
Show older comments
I need to be able to read data from a cell which could in theory be added to.
Currently my cell is as an example of the format:
Results = {'ast' 'utd' 1; 'spu' 'ars' 2; 'cit' 'lec' 1; 'che' 'liv' 2;
'ast' 'spu' 2; 'utd' 'ars' 1; 'cit' 'che' 2; 'lec' 'liv' 1}
I need to be able to create a new variable for each different variable within the cell and set this variable to zero. If there is a 1 in column three of the cell then I need the variable in the first column of that row to be increased by 1, if there is a 2 in column three then I need the variable in column two of that row to be increased by 1.
What code would you recomend to do this? I am not very confident with Matlab but think it is the best program to work with large amounts of data. If anyone could offer some advice, or even better a solution it would be much appreciated.
2 Comments
Stephen23
on 16 Oct 2019
Edited: Stephen23
on 16 Oct 2019
"I need to be able to create a new variable for each different variable within the cell"
The code you show only has one variable, a cell array named Results. The variable Results is a cell array which contains character vectors and numeric scalars (not variables).
"...but think it is the best program to work with large amounts of data"
I doubt that magically creating new variables would be a good way to handle any amount of data, but given that your terminology is quite unclear, it really depends on what you mean by "create a new variable".
It would probably help most if you provided the exact and complete expected output for the input data that you have shown us in your question.
Answers (1)
Guillaume
on 16 Oct 2019
Edited: Guillaume
on 16 Oct 2019
I need new variables created for each of the character vectors
No! As Stephen already told you this would worst way to go about it. See Stephen's excellent post about this to see all the reasons why.
There are many ways you could store your data without ever creating new variables which one is best would depend on what you're planning to do afterward. You can use maps, cell arrays, tables, even structures with dynamic field names (I wouldn't recommend that one, it's hardly better than dynamic variables). I'm going to create a table here:
%demo input. Not a very good input since each 'variable' ends up as being incremented once.
Results = {'ast' 'utd' 1; 'spu' 'ars' 2; 'cit' 'lec' 1; 'che' 'liv' 2;
'ast' 'spu' 2; 'utd' 'ars' 1; 'cit' 'che' 2; 'lec' 'liv' 1};
%identify unique values in first two columns, and their position in the unique array
[variables, ~, loc] = unique(Results(:, [1, 2]));
loc = reshape(loc, [], 2); %reshape to match the original Results shape
%find which 'variables' need to be incremented, 3rd column of Results tells which loc column to increment
toincrement = loc(sub2ind(size(loc), (1:size(loc))', cell2mat(Results(:, 3))));
%compute the histogram of toincrement, many ways to do this (histcount, hist, accumarray, a loop)
count = accumarray(toincrement, 1, size(variables));
%result:
out = table(variables, count)
edit: Another option for storage is to use a table with named rows. This uses the same code as above but construct the table with:
out = table(count, 'RowNames', variables)
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!