How can I create a data table and export it as an excel file?

11 views (last 30 days)
bobby
bobby on 4 Oct 2021
Commented: bobby on 10 Oct 2021
Hello all!
I am having trouble converting my data into a table and exporting it as an excel file.
Essentially, I am importing multiple .mat matrix files, and I am turning that data into basic statistical observations for each file. I wish to tabulate all of these values from a for loop.
Here is the simplified code I have so far
datadirectory = '/Users/name/Desktop/data/';
import = dir(fullfile(datadirectory,'*.mat'));
save_directory = uigetdir('/Users/name/Desktop/data/');
for i = 1:length(import)
Data = load(fullfile(import(i).folder, import(i).name));
histogram(Data.importData)
mean = mean(Data.importData, 'all');
stdev = std(Data.importData, 0, 'all');
median = median(Data.importData, 'all');
mode = halfrange(Data.importData);
fileName = strrep(import(i).name,'.mat','')
end
What I would like to do is make a table of the filename, its respective mean, and its respective standard deviation for each loop. I understand that you would have to use the "table(...)" function, however I am unsure how to convert looped results into it. I tried the following and it doesn't work.
tabData = table( filename(i), mean(:), stdev(:) )
Thank you all for your help!

Accepted Answer

Kevin Holly
Kevin Holly on 5 Oct 2021
Edited: Kevin Holly on 5 Oct 2021
Hey bobby! Glad to see you again!
Here you are:
datadirectory = '/Users/name/Desktop/data/';
import = dir(fullfile(datadirectory,'*.mat'));
save_directory = uigetdir('/Users/name/Desktop/data/');
for i = 1:length(import)
Data = load(fullfile(import(i).folder, import(i).name));
histogram(Data.importData)
mean = mean(Data.importData, 'all');
stdev = std(Data.importData, 0, 'all');
median = median(Data.importData, 'all');
mode = halfrange(Data.importData);
fileName = strrep(import(i).name,'.mat','_stats.xls')
%Create Table
tabData = table(mean,stdev,median,mode)
%Write Table to Excel
writetable(tabData,fullfile(save_directory,fileName),'Sheet',1)
end
  6 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!