Algebra function with table and vector
Afficher commentaires plus anciens
Hi guys! I have 1 table (Composition) and 1 vector (S), and I want to compute the following function: Composition(i,j)=Composition(i,j)./S(i)*100.
For this, I tried to use a loop (a and b are the size of Composition and S, respectively):
i = 1;
j = 1;
for i = i:a
for j = j:b
Composition(i,j)= Composition(i,j)./S(i)*100;
j = j+1;
end
i = i+1;
end
I get the following error: >> import_excel Undefined operator './' for input arguments of type 'table'.
Error in import_excel (line 73) Composition(i,j)= Composition(i,j)./S(i)*100;
I'm at begginer level, so any help is most appreciated! Thanks!

<<

>>
2 commentaires
KSSV
le 3 Oct 2016
How you have read the excel data into MATLAB?
Ana Castanheiro
le 3 Oct 2016
Réponse acceptée
Plus de réponses (2)
See following example:
>> a = [1;2;3;4;5;6];
>> b = a+1;
>> c = a*0;
>> T = table(a,b,c)
T =
a b c
_ _ _
1 2 0
2 3 0
3 4 0
4 5 0
5 6 0
6 7 0
>> bsxfun(@plus,T,a)
Undefined function 'bsxfun' for input arguments of type 'table'.
>> varfun(@(col)plus(a,col),T)
ans =
Fun_a Fun_b Fun_c
_____ _____ _____
2 3 1
4 5 2
6 7 3
8 9 4
10 11 5
12 13 6
For your problem, it would look like:
varnames = Composition.Properties.VariableNames; % Store original variable names.
Composition = varfun(@(column)column./S*100, Composition);
Composition.Properties.VariableNames = varnames; % Restore original variable names.
KSSV
le 3 Oct 2016
0 votes
doc xlsread......
Read the data of the excel file using xlsread...then the data will be stored as numbers. You can do all the algebraic operations.
2 commentaires
Ana Castanheiro
le 3 Oct 2016
Guillaume
le 3 Oct 2016
That is really not a good answer!
The data is stored as numbers, just as a table. The issue has nothing to do with the way the data is read. It's a simple matter of indexing a table.
Since this is linked to the previous question, I would add that rather than using xlsread followed by celltotable, it would be simpler to read the file with:
Composition = readtable(FilePath, 'Sheet1', 'A1:ZZ1');
which will create the table in one go, and use row 1 as the table column name.
readtable is a lot more powerful and flexible than xlsread, so there's really no reason to use xlsread anymore.
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!