How to retrieve tables from a cell array
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
assuming we have some data files and we are storing the data of these files in a cell array called "results", the cell array contains two columns the first is for the name of the file and the second one we have each cell contains multiple tables corresponding to a data file. (see the images below).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/749684/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/749689/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/749694/image.png)
Now, assuming that each table of these has a unique "Date" value for the data in it, How can we go through all the cells in the cell array for all the files we have to retrieve the tables that there "Date" value is between for example (196611110928 and 196611110940) or any other values?
I was able to sort this out for having a specific value of the Date, i.e for retrieving a single table, but when having a certain range of the Date values and retrieving multiple tables I am not sure how to do that.. anyone can help?
0 commentaires
Réponse acceptée
Shanmukha Voggu
le 28 Sep 2021
Hi Osama,
I understood that you want to know
1)how to loop over all cells in the cell array(results)
2)how to loop over all the tables in the cell
According to question I understood that if every row of table has the Date in the specific range for example (196611110928 and 196611110940), then that table can be retrieved
The finalCell is the cell array that contains all the resultant tables
finalCell={};% contains all tables having Date range (196611110928 ,196611110940)
for i=1:size(results,2)% "results" is the cell array mentioned in question
currCell=results{i,2};% current cell in the iteration
for j=1:size(currCell,1)
currTable=currCell{j,1};% current table in the iteration
booleanMatrix=(boolean(ones(size(currTable,1))))';% creating a Boolean matrix that contains all logical 1's
if((currTable.Date>196611110928 & currTable.Date<196611110940)==booleanMatrix)% This condition will true when the Date is in given range
finalCell{end+1}=currTable;% if above condition is true, then add the table to cell array
end
end
end
Note: Before executing following code make sure to have results variable in the workspace.
5 commentaires
Stephen23
le 29 Sep 2021
Modifié(e) : Stephen23
le 29 Sep 2021
currTable.Date>196611110928 & currTable.Date<196611110940)==booleanMatrix
% This comparison is completely superfluous ^^
The LE and LT comparisons return TRUE/FALSE values, which the pointless EQ comparison compares against a vector of ONES to return exactly the same TRUE/FALSE values.
Do not learn from such pointlessly obfuscated code.
@Shanmukha Voggu: BOOLEAN is not a MATLAB function, LOGICAL is a MATLAB function. Rather then inventing functions that do not exist it is much more reliable to read the MATLAB documentation.
Hailey Hayes
le 15 Juil 2022
I have a similar use case. I used this to help sort my tables and now I need to take a time difference within the sorted tables. The sorted tables are in their own cell array. Is there a way to perform subtraction on multiple tables within a cell array using a for loop?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!