# loop for excel in MATLAB

4 views (last 30 days)
Samer Husam on 7 Jun 2012
hi all, I want to save values from MATLAB to excel so I want to write a loop to check if the cell has a value or not, for example: lets consider the column (B) I want the matlab to start checking from B4 which one is empty cell to save the new values in it, how this can be done ?? looking for your help please.
per isakson on 7 Jun 2012
Did you look in the Matlab on-line help?
Samer Husam on 7 Jun 2012
I tried to, but couldn't find the way to do it,
actually i could write the code for the loop in order to check the value for a cell.

Samer Husam on 8 Jun 2012
thanks all for your help, but I can't make this work cause its not really clear to me.. so, is there another way to do it ? for example: for column B in (excel) write loop program to check if the cell has value or not ?
%------a:is the variable to save------
for i=5
i+1;
end
xlswrite(G:\\MATLAB Examples\Data_sheet.xlsx',a,'Sheet1','B%i,)
can something like this to be done between matlab and excel ??

Andrei Bobrov on 7 Jun 2012
one way
nt(cellfun(@isnan,nt)) = {256};
xlswrite('nameyour.xlsx',nt)
eg, data:
xlswrite('Data_sheet.xlsx',[1;8;1;6;1],'B1:B5')
solution
a = 100;
n(n == 1) = a;
xlswrite('Data_sheet.xlsx',n,'B1:B5')
##### 3 CommentsShow 1 older commentHide 1 older comment
Image Analyst on 8 Jun 2012
He's setting those Excel cells (which are now in a MATLAB cell array called nt) that are not numbers to have the new value of 256. You can change the 256 to whatever number you want. Then it writes if back out to the same file. If you want just some submatrix (like B4..F42), then you'd have to extract that submatrix before running andrei's code on just that submatrix. Then insert the submatrix back into the full matrix and write out the full matrix.
Andrei Bobrov on 8 Jun 2012
Thank you Image Analyst! Such a good explanation difficult for me (English is not my mother tongue).