Filter löschen
Filter löschen

having char and number in one matrix

4 Ansichten (letzte 30 Tage)
Roozbeh Yousefnejad
Roozbeh Yousefnejad am 28 Mai 2018
Kommentiert: Walter Roberson am 28 Mai 2018
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

Antworten (1)

Walter Roberson
Walter Roberson am 28 Mai 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 Kommentare
Roozbeh Yousefnejad
Roozbeh Yousefnejad am 28 Mai 2018
Thanks, then with this approach can I finally export the table you've mentioned or the cells to as a CSV file?
Walter Roberson
Walter Roberson am 28 Mai 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.

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by