Struggling with multiple csv files

1 view (last 30 days)
Hello everyone,
I am new to working with reading files in matlab. So, my automated testing generated multiple files like "... rpm_Variation1", "... rpm_Variation2" and so on. Now I came up with the following code:
clear all;
close all;
clc
%%First part just to test if it works
file = 'C:\Users\Brendan\OneDrive\Masterarbeit_prädiktive_Stromregler\Modelle\01_Parameter\01_Ld\rpm\01_Ld_FCS_MPCCC_rpm_Variation1.csv';
A = readmatrix(file);
x = num2cell(A, 1)
t = x{1}
%%Second part for reading in multiple files
Drpm = 'C:\Users\Brendan\OneDrive\Masterarbeit_prädiktive_Stromregler\Modelle\01_Parameter\01_Ld\rpm';
files = dir (fullfile(Drpm, '*.csv'));
for i = 1:length('files')
datarpm = readmatrix(files(i).name);
end
The code above gave me following errors:
Error using readmatrix (line 158)
Unable to find or open '01_Ld_FCS_MPCCC_rpm_Variation1.csv'. Check the path and filename or file permissions.
Error in Variation_Ld (line 31) datarpm = readmatrix(files(i).name);
Unfortunately, I don't have an idea how to correct these errors. Does anyone have an idea?
Thank you in advance!

Accepted Answer

Stephen23
Stephen23 on 31 May 2023
Edited: Stephen23 on 31 May 2023
"I don't have an idea how to correct these errors. Does anyone have an idea?"
The error message already tells you how: "Check the path and filename or file permissions."
You told DIR, but forgot to tell READMATRIX to look in the correct location.
Drpm = 'C:\Users\Brendan\OneDrive\Masterarbeit_prädiktive_Stromregler\Modelle\01_Parameter\01_Ld\rpm';
files = dir(fullfile(Drpm, '*.csv'));
for i = 1:numel(files) % fixed bug
fnm = fullfile(Drpm,files(i).name); % added
datarpm = readmatrix(fnm); % modified
end

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!