Borrar filtros
Borrar filtros

Why Matlab doesn't export the result?

2 visualizaciones (últimos 30 días)
Dung Le
Dung Le el 4 de En. de 2016
Comentada: Walter Roberson el 5 de En. de 2016
I import an excel file into Matlab. Then I expect with the code I write in Matlab, it will export the result in column 3 in the original excel. But nothing appears in column 3 of the file sample2.xlsx. Could you help me?
clear all
clc
A=xlsread('Criteria.xlsx');
B=xlsread('sample2.xlsx');
n=0;
x=0;
for h=1:99
if B(h,2)>50
n=n+1;
x= x+B(h,3);
end
end
x=x/n;
for h=1:99
if B(h,2)>50
B(h,4)=x;
end
end
xlswrite('sample2.xlsx',B)

Respuesta aceptada

Image Analyst
Image Analyst el 5 de En. de 2016
What is "A" for? You're not even using it.
And column 3 of B has NaNs in it, so x will be a NaN also, which means column 4 is all NaNs. It looks like x is supposed to be the mean of Col 3 of B for rows where B > 50. Is that true? If so, those two for loops can be vectorized:
% Don't read in A - it's not used.
% A=xlsread('Criteria.xlsx');
B=xlsread('sample2.xlsx');
% Determine which rows to average.
rowsToAverage = B(:,2)>50 & ~isnan(B(:,3));
% Get mean in Column 3
meanOfBigNumbers= mean(B(rowsToAverage, 3));
% Set col 4 = means
B(rowsToAverage, 4) = meanOfBigNumbers;
% Overwrite workbook with new B value.
xlswrite('sample2.xlsx',B)
The xlswrite should work.
  5 comentarios
Image Analyst
Image Analyst el 5 de En. de 2016
Looks like you've found a solution in your parallel thread because you've accepted an answer there. I still claim you don't need all those for loops. But anyway, at least it's working.
Walter Roberson
Walter Roberson el 5 de En. de 2016
Dung Le remarks "Thank you your enthusiastic support :)"

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 5 de En. de 2016
Could you confirm that you want to take the mean of column 3 over the places that column 2 is > 50, and that you want to write copies of that mean to column 4 for those same rows? Like
mask = B(1:99,2) > 50;
x = mean(B(mask,3));
h(mask,4) = x;
  3 comentarios
Image Analyst
Image Analyst el 5 de En. de 2016
The code in my answer (different than Walter's code in this Answer) does work - I actually tried it. There is stuff in all 4 columns. Where you originally had NaN's there will be a blank cell.
Dung Le
Dung Le el 5 de En. de 2016
Thanks :)

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by