Writing in an existing Excel file
Afficher commentaires plus anciens
I am making a GUI with the AppDesigner in Matlab. The app needs to be able to run multiple times, each time the results need to be published in the same Excel file. Except, when running the app again, the data in the Excel is lost and only new data added to the file, even when manually saving the Excel file. These are my callbacks:
Does anyone know why it is not overwriting the existing Excel file?
% Callbacks that handle component events
methods (Access = private)
% Value changed function: SamplenumberEditField
function SamplenumberEditFieldValueChanged(app, event)
value = app.SamplenumberEditField.Value;
end
% Button pushed function: StartButton
function StartButtonPushed(app, event)
ind = app.SamplenumberEditField.Value;
filename = 'Format.xlsx';
table_starttimes = cell(100,1);
table_starttimes{ind} = datestr(now, 'HH:MM:SS');
xlswrite('Format.xlsx',table_starttimes,'Sheet1','F2')
end
% Button pushed function: SnapshotButton
function SnapshotButtonPushed(app, event)
im = imread('image.tif');
ind = app.SamplenumberEditField.Value;
filename = 'Format.xlsx';
table_sample = cell(100,1);
table_name = cell(100,1);
table_amount = cell(100,1);
table_conc = cell(100,1);
table_endtimes = cell(100,1);
table_volume = cell(100,1);
table_inside = cell(100,1);
table_outside = cell(100,1);
table_sample{ind} = ind;
table_name{ind} = 'name';
table_volume{ind} = 2;
table_inside{ind} = 2;
table_outside{ind} = 2;
%% Save endtime and concentration to excel file
amount=f_calculate()
binder_concentration=amount/2
table_amount{ind} = amount;
table_conc{ind} = binder_concentration;
table_endtimes{ind} = datestr(now,'HH:MM:SS');
xlswrite('Format.xlsx',table_name,'Sheet1','A2')
xlswrite('Format.xlsx',table_sample,'Sheet1','B2')
xlswrite('Format.xlsx',table_volume,'Sheet1','C2')
xlswrite('Format.xlsx',table_outside,'Sheet1','D2')
xlswrite('Format.xlsx',table_inside,'Sheet1','E2')
xlswrite('Format.xlsx',table_endtimes,'Sheet1','G2')
xlswrite('Format.xlsx',table_amount,'Sheet1','H2')
xlswrite('Format.xlsx',table_conc,'Sheet1','I2')
winopen('Format.xlsx')
end
end
2 commentaires
dpb
le 28 Juil 2021
"when running the app again, the data in the Excel is lost and only new data added to the file, even when manually saving the Excel file. These are my callbacks:..."
"Does anyone know why it is not overwriting the existing Excel file?"
Those two statements are contradictory.
Please clarify which is the desired result -- and what, specifically is wrong are you seeing that isn't as desired?
It surely looks as though the above would insert all new data into the same file at the same locations each time it is run.
I would, however, strongly recommend to restructure your code to put the data into a column array and write the array once instead of calling xlswrite multiple times--this is a real bottleneck and unnecessary.
Also, use writematrix instead of the deprecated xlswrite
Giulia Pötgens
le 29 Juil 2021
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Spreadsheets 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!