- when you read the dir help, it clearly lists all of the fields of the output structure: name, folder, bytes, etc. It does not list fields like pmu6n7angoffset1.csv or anything similar.
- That syntax would not work anyway, because you wrote a character vector where there should be a fieldname (e.g. name, folder, bytes, etc). The MATLAB documentation explains how to access the fields of structures.
- What is the point of hard-coding the name anyway? The whole point of dir is that it automatically collects all the filenames for you, so that you don't need to write filenames everywhere!
- Why are you using cell array indexing to try and access a structure?: files{ii}....
- Your text states that you want "...the 4th column of each csv file...", so why are you getting the second column?: new(:,2));
I have multiple CSV files and I am trying to make a new CSV file by combining the 4th column of each of the CSV file... but cant figure out how..csvwrite command is a bit confusing for me...Can someone please tell me how can I do that?
1 view (last 30 days)
Show older comments
So i need to make a new csv file using a multiple csv files (only the 4th column of each csv file specifically and the first column is common in all....which i also want to be the 1st column of new csv file) but i am just stuck with constant errors i tried with this code
files= dir('*.csv');
out = csvread(files(1).'pmu6n7angoffset1.csv');
for ii = 2:numel(files)
new = csvread(files{ii}.'pmu6n7angoffset10.csv');
out = horzcat(out, new(:,2));
end
but it doesnt work
1 Comment
Stephen23
on 31 Aug 2018
You really need to start reading the MATLAB documentation, because guessing how to write code is just going to be huge waste of your time.
Answers (1)
Stephen23
on 31 Aug 2018
Edited: Stephen23
on 31 Aug 2018
Instead of guessing how to write MATLAB code it is much easier to follow the examples in the documentation:
Try something like this:
S = dir('*.csv');
C = cell(1,numel(S));
for k = 1:numel(S)
tmp = csvread(S(k).name);
C{k} = tmp(:,2); % 2 or 4 ?
end
M = horzcat(tmp(:,1),C{:}}
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!