Brace indexing is not supported for variables of this type.

I am getting the following error for the attached code.
Error in Readfiles (line 8)
data = readmatrix(fnames{idx});
Could you please help me out.

 Risposta accettata

fname is a struct, not a cell array. Change the line to
data = readmatrix(fnames(idx).name);

9 Commenti

Dear Ameer Hamza, Thank you so much for the quick reply.
I am glad to be of help!
Dear Ameer Hamza, the previous programme didn't read the .csv files in an order. I used 'natsortfiles' function to read them in proper order. But I am getting an error.
Dot indexing is not supported for variables of this type.
Error in Readfiles (line 11)
data = readmatrix(fnames(idx).name);
This code does not contain natsort. How did you sorted the struct array 'fnames'?
Please go through the second time attached code.
Yes, I am referring to the file attached in your comment. It does not have a call to natsortfiles().
I am really sorry, I have attached a wrong file previously. Here is the right file.
This code does not contain the line
data = readmatrix(fnames(idx).name);
as shown in your previous comment. In any case, N is a cell array in your case and you can acess the filenames like this
data = readmatrix(N{idx});
Thanks for your time. I resolved the problem.

Accedi per commentare.

Più risposte (1)

% List all CSV files
fnames = dir('*.csv');
% Pre-allocate output vector
ranges = zeros(numel(fnames), 1);
% Loop over file names
for idx = 1:numel(fnames)
% Read the CSV data
data = readmatrix(fnames(idx).name);
% Get the 5th column
col5 = data(:,5);
% Get the range for this file
ranges(idx) = max(col5) - min(col5);
end
% Write the result
writematrix(ranges, 'ranges.csv');

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by