How do I write a function where an input of multiple matrices is incorporated into a single matrix as the output?
2 次查看(过去 30 天)
显示 更早的评论
This seems like a fairly simple problem since it is simple (although tedious) to do by hand in excel. However, in dealing with larger data sets (of more than 1000 species), it would be much more efficient/reasonable to have an automated program or function that I can just apply to a whole excel file and have it run through and return the matrix so the real data visualization and analysis can be done sans brute force prep work.
Input: Excel file with multiple sheets. Each sheet is one sample. In each sheet, the first column has nominal categorical data (species), followed by multiple columns of numerical data, of which the only relevant column is (amount); the amount is the number of each species present in each sample. The species data differs quantitatively and qualitatively in every sample; for instance, sample 1 could have dog (amt=9) and cat (amt=8), while sample 2 has dog (amt=7), fish (amt=6), and turtle (amt=5), and sample 3 has cat (amt=4), turtle (amt=3), penguin (amt=2), and dragon (amt=1).
Analysis/output: Function that extracts species and amount data from an excel file with multiple sheets to construct a matrix in which each row (i?) corresponds to each unique species (order doesn't matter, but no doubles) and each column (j?) corresponds to each sample, and the value for each element in the matrix is either 0 (if the species is not present in that sample) or n (where n=amt of that species in that sample).
Can someone point me in the right direction of how to do this?
1 个评论
Stephen23
2017-4-19
编辑:Stephen23
2017-4-19
Preallocate an array (one that suits your data, e.g.: table, cell, ND numeric, ...), loop over the files, import the data, add data to the array.
It would probably be simpler to import all of the data first, and perform the summing/unique operations once the data is in MATLAB memory.
回答(1 个)
Dimitris Iliou
2017-4-21
If I understand your question correctly, you are trying to import data from an excel file that has multiple sheets, and then find the unique categories and create a category vs sample (amount) matrix.
The first step would be to read the data from the excel file. To do that, you can use the 'xlsread' command. You can find more details on how the command works in the following link:
I would use the
[num,txt,raw] = xlsread(filename,sheet)
format to get the 'raw' cell array back, that contains all your categories and data.
After reading all your data, you can include all the species into a single cell array and take a unique set of that. You can find more information on the 'unique' command in the following documentation link:
Finally, you can create a small script that creates a table and traverses all the data to populate it. You can find more information on 'table' in the following link:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!