Extracting edited tables from cell array and saving them back to original variables
1 view (last 30 days)
Show older comments
CHRISTOPHER MILLAR
on 5 Apr 2019
Commented: CHRISTOPHER MILLAR
on 5 Apr 2019
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
0 Comments
Accepted Answer
Guillaume
on 5 Apr 2019
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 Comments
Guillaume
on 5 Apr 2019
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).
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!