Creating new matrices with input depending on csv filename

Hi.
I have a bunch of csv files containing charge and discharge data that I would like to separate into a charge matrix and a discharge matrix.
The filenames in the folder for the charge data are CYCLE0_CHG, CYCLE2_CHG, CYCLE4_CHG, etc. and the filenames for the discharge data are CYCLE1_DIS, CYCLE3_DIS, CYCLE5_DIS, etc.
The following is my initial step at dividing the files into 2 variables based on their file name.
filepath = 'C:\Kikusui testing\12s2p Cycling\CH1_2023_3_6_14_32';
fileinfo1 = filepath(dir('_CHG.csv'));
fileinfo2 = filepath(dir('_DIS.csv'));
However, I am getting the error "Unable to use a value of type struct as an index."

 Réponse acceptée

Stephen23
Stephen23 le 22 Mar 2023
Modifié(e) : Stephen23 le 22 Mar 2023
If you want those file names to be sorted into alphanumeric order then you will either need to generate the filenames (e.g. SPRINTF or similar) or sort the output from DIR:
P = 'C:\Kikusui testing\12s2p Cycling\CH1_2023_3_6_14_32';
C = dir(fullfile(P,'CYCLE*_CHG.csv'));
D = dir(fullfile(P,'CYCLE*_DIS.csv'));
C = natsortfiles(C); % download: https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
D = natsortfiles(D); % download: https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
for k = 1:numel(C)
F = fullfile(P,C(k).name);
C(k).data = readmatrix(F); % or READTABLE, etc.
end
for k = 1:numel(D)
F = fullfile(P,D(k).name);
D(k).data = readmatrix(F); % or READTABLE, etc.
end
Optional, assuming compatible arrays sizes and classes:
Cdata = vertcat(C.data); % charge
Ddata = vertcat(D.data); % discharge

4 commentaires

Thank you for your help. I have an additional question.
Without using the vertcat, the code above gives me C and D as struct. How do I then extract data from specific columns/rows wihin the struct?
For example, I would like to get the data for all rows in column 16:25 for C(2:end).
"How do I then extract data from specific columns/rows wihin the struct?"
Note that the structure is already very easy to access, for example the 2nd file is
S(2).name % filename
S(2).data % imported data
Depending on how you need to process your data, you might be able to simply loop over those structures:
Another approach is to place the data in a cell array and loop over that (or use CELLFUN). For example:
Ccell = {C.data};
Dcell = {D.data};
which you can then easily loop over, or use CELLFUN:
F = @(m) m(:,16:25); % all rows from columns 16 to 25.
Csub = cellfun(F, Ccell, 'uni',0)
Dsub = cellfun(F, Dcell, 'uni',0)
These approaches rely on indexing and on comma-separated lists. Read more here:
Could you please clarify what this line means
F = @(m) m(:,16:25); % all rows from columns 16 to 25.
Is @m just an anonymus function and if so, why do I use that?
I used the code above and received the following error: Index in position 2 exceeds array bounds. Index must not exceed 1.
"Could you please clarify what this line means F = @(m) m(:,16:25); % all rows from columns 16 to 25."
F is the handle to an anonymous function. The anonymous function takes one input argument m (a matrix) and returns all rows for columns 16 to 25 of the input matrix.
"Is @m just an anonymus function and if so, why do I use that?"
@m does not exist in my code.
I specified the anonymous function so that I could use CELLFUN to process all cells of Ccell and Dcell. As an alternative you could always use a FOR loop.
"I used the code above and received the following error: Index in position 2 exceeds array bounds. Index must not exceed 1."
Then one (or more) of your matrices has only one column, not 25 columns. MATLAB will throw an error if you ask it for the content of the 25th column from a matrix with only one column.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2022b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by