Save multiple columns of multiple csv files?

10 views (last 30 days)
I have the following code:
[file_list, path_n] = uigetfile('.csv', 'Grab csv', 'Multiselect', 'on')
if ismatrix(file_list) == 0
file_list = {file_list}
for i = 1:length(file_list)
filename = file_list{i}
data_in = readmatrix([path_n, filename]);
% here I would like to import columns 7-27 for each file
% but instead of saving only the last iteration of the loop I want to
% save each in a new variable
columns(i) = data_in(:,7:27);
Every time I run this I get an error that I dont understand.
Unable to perform assignment because the left and right sides have a different number of elements.
Error in test (line 10)
columns(i) = data_in(:,7:27);
I have figured out how to get the 21 columns saved in columns but it only saves the last iteration. How do I save each iteration?
Help would be much appreciated.
  1 Comment
Stephen23 on 21 Jan 2022
Simpler way to ensure that the output is a cell array of character vectors:
[file_list, path_n] = uigetfile(...);
file_list = cellstr(file_list); % <- easy

Sign in to comment.

Accepted Answer

Voss on 21 Jan 2022
[file_list, path_n] = uigetfile('.csv', 'Grab csv', 'Multiselect', 'on');
if ~iscell(file_list)
file_list = {file_list};
columns = cell(length(file_list),1);
for i = 1:length(file_list)
filename = file_list{i};
data_in = readmatrix([path_n, filename]);
columns{i} = data_in(:,7:27);
% if you want to put all the data into one matrix:
columns = cell2mat(columns);
lil brain
lil brain on 22 Jan 2022
Hey Benjamin, wow thanks for the insightful answer and the good directions as well. Very much appreciated! I will have a go at implenting it as a cell array then. I havent had to much experience witht he different formats and so I just kind of used what I knew. Thanks again!

Sign in to comment.

More Answers (0)


Find more on Files and Folders 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!

Translated by