having char and number in one matrix
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Walter Roberson
el 28 de Mayo de 2018
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 comentarios
Walter Roberson
el 28 de Mayo de 2018
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.
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!