Extracting edited tables from cell array and saving them back to original variables

Im sure there is a simple method for doing this but I cant seem to figure it out
I have created cell array full of pre-existing tables
myTables = {indxExt,indxFlex,lrgeDia,midExt,midFlex,pinch}
Then I loop through the myTables variable and remove the data that i do not require from each matrix in each cell and update myTables
for i = 1:length(myTables)
neg = myTables{i}(1,i)<0;
if neg == 1
data = myTables{i};
data(:,data(1,:)<0) = [];
myTables{i}=data;
end
How can I then save the new tables back to the corresponding original table?
Thanks

 Réponse acceptée

Note
if logicalvalue == 1
is the same as asking if true is true. It's a bit redundant. In addition in matlab it involves converting the logical value to double, to compare it to the double 1. The result of the comparison is then the exact same logical value you started with.
if logicalvalue
does the same more succintly and without unecessary conversions.
It doesn't look like your tables are actual matlab tables. The syntax you're using wouldn't work with tables, so I'm assuming you're using matrices.
The code you've written does store the modified matrices in the original cell array. However, you only modify said matrices, if the 1st element of column i of the ith matrix is negative. Is it really what you intended?
If you intended to delete all columns whose 1st element is negative, from all the matrices, then you don't need that if:
for i = 1:numel(myTables)
mytables{i}(:, myTables{i}(1, :) < 0) = []; %you don't even need to bother with the temporary data
end
which you can also write with a cellfun:
mytables = cellfun(@(m) m(:, m(1, :) >= 0), myTables, 'UniformOutput', false); %only keep columns whose first value is >= 0

6 commentaires

Thanks Guillaume for your help
Yes i wanted to remove all rows from the column in the matrix where the 1st column has a negative number but my problem at the minute is when i remove the data from the matrix in postition i and update in the corresponding myTables variable how do i update the original variable that was used to create myTables{i}
how do i get indxExt to now equal the updated matrix with the data from myTables{1} after I have removed the data i do need to be included?
Stephen23
Stephen23 le 5 Avr 2019
Modifié(e) : Stephen23 le 5 Avr 2019
"how do i update the original variable that was used to create myTables{i}"
If you have to do that then your data design is fundamentally flawed.
Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know some reasons why:
The simple and efficient solution is to store your data in the cell array and access it from the cell array using very efficient indexing.
Thanks Stephen
I will read through the thread you linked
So say I want to save the data back to an excel file, am I better to just save the cell array myTables to an excel file, instead of the individual matrices? - OK i undersand this is not possible - ignore this question
Stephen23
Stephen23 le 5 Avr 2019
Modifié(e) : Stephen23 le 5 Avr 2019
"am I better to just save the cell array myTables to an excel file, instead of the individual matrices?"
Every cell of a cell array already contains an "individual matrix" which can certainly be saved to an Excel file (or files), simply by using indexing.
The only thing that you should avoid is magically accessing variable names.
How do you save multiple matrices in the excel file? One per sheet? If so, then yes, read them directly into the cell array, modify them as element of the cell array, and write back the cell array, one cell array per sheet. There's never any need for individual variables.
If on the other hand, it's just one excel sheet that you've split into several matrices, then you would be better off keeping the data together in just one array.
Note that if you're dealing with excel sheets which have column (or row) headers, you may actually be better off using actual matlab tables since this will preserve the headers (as long as the text makes valid matlab variable names otherwise, it'll get mangled slightly).
Im still getting to grips with Matlab so Thanks for all your help today guys

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by