having char and number in one matrix
1 次查看(过去 30 天)
显示 更早的评论
Hi, I am reading some CSV files and do some calculations on them and at the end, I will have the results for each CSV file in one row of matrix N. Now I want to add the CSV files' name in column 10. I can separate the name of the file with fileparts function; however, I cannot put add names to matrix N which had a number. here is my code
clc
clear
files = subdir('C:\Users\roozm\Desktop\New folder\*.csv'); % here specify the path to top folder which contain all the other csv files
N=zeros(numel(files),9);
D=zeros(numel(files),1);
D=num2str(D);
for i=1:numel(files) % for loop will iterate over all file names
filename = files(i).name;
sheet = 1;
[filepath,name,ext] = fileparts(filename);
D(i,1)= name;
subset = xlsread(filename,sheet,'A:C');
subset2 = xlsread(filename,sheet,'F:G');
subset3 = xlsread(filename,sheet,'K:N');
subset_merged=[subset,subset2,subset3];
subset_tot=subset_merged([end-6:end-2],:);
M=mean(subset_tot); % or readtable() or whatever function you are using to read csv file
N(i,:)=M;
end
I have attached a picture of matrix N
0 个评论
回答(1 个)
Walter Roberson
2018-5-28
That is something that is not possible in MATLAB. All of the columns of an array must be the same data type.
You can convert the numbers to a cell array using mat2cell(), and then you can add another column which is a cell array of character vectors that are the names you want.
You could also create a table() object using array2table() and then add in a new column that was the names you wanted.
2 个评论
Walter Roberson
2018-5-28
csvwrite() and dlmwrite() cannot handle mixed cell arrays. writetable() can handle mixed data being written to .csv file.
You could also use fopen()/fprintf()/fclose() to write the .csv file with whatever content you wanted.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!