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

1 回表示 (過去 30 日間)
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

採用された回答

Voss
Voss 2022 年 1 月 24 日
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 件のコメント
Voss
Voss 2022 年 1 月 24 日
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.
enzo Boven
enzo Boven 2022 年 1 月 25 日
I fixed the code it works for me. Thanks for your help!

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2022 年 1 月 24 日
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);

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by