Easy and fast way to export into excel file from loop
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
CCF2017 MIT
il 10 Set 2020
Modificato: CCF2017 MIT
il 10 Set 2020
I want to give output from a nested loop to an excel file.
for k = 1:61
%nested loop here
%then the relevent code for output is:
if c == 0&&c1 == 0&&c2 == 0&&c3 == 0&&c4 == 0
E = 0.84;
elseif c == 1&&c1 == 0&&c2 == 0&&c3 == 0&&c4 == 0
E = 0.88;
elseif c == 1&&c1 == 1&&c2 == 0&&c3 == 0&&c4 == 0
E = 0.92;
elseif c == 1&&c1 == 1&&c2 == 1&&c3 == 0&&c4 == 0
E = 0.94;
elseif c == 1&&c1 == 1&&c2 == 1&&c3 == 1&&c4 == 0
E = 0.95;
elseif c == 1&&c1 == 1&&c2 == 1&&c3 == 1&&c4 == 1
E = 0.96;
else
disp('Not Valid')
end
E_cell = sprintf('D%s', num2str(k));
xlswrite(filename , E, 'Sheet1', E_cell)
end
1 Commento
Johannes Hougaard
il 10 Set 2020
Do you need to append the data to the same Excel file for each k or do you need to create 61 different excel files?
From your code it seems that you wish to write the value of E in column D of Sheet1 of your excelfile filename. Is that a correct assumption?
And again - I'd assume what happens is that your value for k = 61 is written in cell D61 of your Sheet1 and nothing else is stored in your excel file.
Risposta accettata
Johannes Hougaard
il 10 Set 2020
You should concatenate your E variable before writing the excelfile as xlswrite creates a file rather than edits a file. As an added benefit this will be substantially faster as you only do one call to xlswrite and will allow you to inspect your variable E_out as well.
filename = "thisisanewexcelfile.xlsx";
E_out = nan(61,1);
E_cell = "D1";
for k = 1:61
%nested loop here
%then the relevent code for output is:
if c == 0&&c1 == 0&&c2 == 0&&c3 == 0&&c4 == 0
E = 0.84;
elseif c == 1&&c1 == 0&&c2 == 0&&c3 == 0&&c4 == 0
E = 0.88;
elseif c == 1&&c1 == 1&&c2 == 0&&c3 == 0&&c4 == 0
E = 0.92;
elseif c == 1&&c1 == 1&&c2 == 1&&c3 == 0&&c4 == 0
E = 0.94;
elseif c == 1&&c1 == 1&&c2 == 1&&c3 == 1&&c4 == 0
E = 0.95;
elseif c == 1&&c1 == 1&&c2 == 1&&c3 == 1&&c4 == 1
E = 0.96;
else
disp('Not Valid')
end
E_out(k) = E;
end
xlswrite(filename , E_out, 'Sheet1', E_cell);
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Spreadsheets in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!