How can I create a data table and export it as an excel file?
    조회 수: 10 (최근 30일)
  
       이전 댓글 표시
    
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!
댓글 수: 0
채택된 답변
  Kevin Holly
    
 2021년 10월 5일
        
      편집: Kevin Holly
    
 2021년 10월 5일
  
      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
  Kevin Holly
    
 2021년 10월 6일
				Need to convert the character array to string array. For character arrays, each character is an element in the array. For string arrays, everything within the quotes is one element regardless of how many characters.
You cannot combine character arrays of unequal size together in a column. In this case, you had one that had 60 characters and another that had 62. By converting them to string arrays, the element size of each is 1, similar to that of a cell array.
I had forgotten to update this line. I also removed the file extension from the name.
 fileName(i,:) = convertCharsToStrings(strrep(import(i).name,'.mat','')
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
			
	제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

