How to create an output excel file after using uitgetfile to select multiple files?

2 views (last 30 days)
enzo Boven
enzo Boven on 24 Jan 2022
Commented: enzo Boven on 25 Jan 2022
I used uitgetfile with multiselect to perform a calculation for multiple trials at once. I want to create an output file in excel that contains the answers of a calculated variable per trial in one column. With the script used right now, the answers will be overwritten by the following selected file.
script used:
[file_list,path_name] = uigetfile('.xlsx','MultiSelect','on');
if iscell(file_list) == 0
file_list = {file_list};
end
for i = 1:length(file_list)
filename = file_list{i};
data_in = xlsread([path_name, filename]);
x_rf_raw = data_in(:,9); %9 relative xrf data else 1
y_rf_raw = data_in(:,10); %10 relative yrf data else 2
x_s_raw = data_in(:,12); %12 relative xs data else 5
y_s_raw = data_in(:,13); %13 relative ys data else 6
% calculations.....
% Calculated variable (example)
Sum = x_rf_raw + y_rf_raw
%==================== Output ====================
defaultfilename = ['Sum.xlsx'];
defaultoutpathname = [];
[fileout, pathout] = uiputfile([defaultoutpathname,defaultfilename], 'Excel file Save As');
Results_names = {'Sum x and y'};
Results_values = [Sum]
sheet = 1;
xlrange = 'B1';
xlswrite(defaultfilename,Results_values,sheet,xlrange);
end

Accepted Answer

_
_ on 24 Jan 2022
Collect each file's "Sum" in a cell array inside the loop, and then write it to file after the loop:
[file_list,path_name] = uigetfile('.xlsx','MultiSelect','on');
if iscell(file_list) == 0
file_list = {file_list};
end
Sum = cell(length(file_list),1);
for i = 1:length(file_list)
filename = file_list{i};
data_in = xlsread([path_name, filename]);
x_rf_raw = data_in(:,9); %9 relative xrf data else 1
y_rf_raw = data_in(:,10); %10 relative yrf data else 2
x_s_raw = data_in(:,12); %12 relative xs data else 5
y_s_raw = data_in(:,13); %13 relative ys data else 6
% calculations.....
% Calculated variable (example)
Sum{i} = x_rf_raw + y_rf_raw;
end
%==================== Output ====================
defaultfilename = ['Sum.xlsx'];
defaultoutpathname = [];
[fileout, pathout] = uiputfile([defaultoutpathname,defaultfilename], 'Excel file Save As');
Results_names = {'Sum x and y'};
all_in_one_column = true;
if all_in_one_column
% to make all results in one column:
Results_values = cell2mat(Sum);
else
% to make each trial its own column:
N = cellfun(@numel,Sum);
Results_values = NaN(max(N),numel(Sum));
for i = 1:numel(Sum)
Results_values(1:N(i),i) = Sum{i};
end
end
sheet = 1;
xlrange = 'B1';
xlswrite(fullfile(pathout,fileout),Results_values,sheet,xlrange); % use the specified output file
  7 Comments

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 24 Jan 2022
You have the uiputfile() to create or select a new file, you just need to utilize it. Change the last line to
xlswrite(fullfile(pathout,fileout),Results_values,sheet,xlrange);

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by