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

1 view (last 30 days)
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

Voss
Voss 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
Voss
Voss on 24 Jan 2022
Possibly. I can't say for sure because I haven't seen how you use corrcoef() (not coerrcoef()) in your script.
It is difficult for me to help if you do not post the actual code that is giving an error.
Read about cell arrays.

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!