Why I can't open csv from matlab live script?

5 visualizaciones (últimos 30 días)
Sándor Burian
Sándor Burian el 26 de Sept. de 2021
Comentada: Walter Roberson el 26 de Sept. de 2021
Hi, I'm newbie, so this is maybesomething basic, but I want to iterate trough a folder with .csv's and open them with readmtrix() but I got Error using readmatrix (line 146) Expected a string scalar or character vector for the parameter name error for this.
Here is my code:
path = uigetdir();
folder_read = dir(strcat(path,'\my\path\extended\*.csv'));
for i = 1:numel(folder_read)
Data = readmatrix(folder_read(i).name,1,0); %here is the error.
end;
When I type to console >> class( folder_read(i).name) I got: ans = 'char'
What do I need to do to make it work?
  2 comentarios
Ravi Narasimhan
Ravi Narasimhan el 26 de Sept. de 2021
Editada: Ravi Narasimhan el 26 de Sept. de 2021
I looked at the readmatrix documentation and was not able to find what the "...1,0);" part corresonds to.
I think the error is saying it is expecting a Name, Value pair. The Name part is usually text in between single quotes.
See https://www.mathworks.com/help/matlab/ref/readmatrix.html and scroll down a bit for the following example with Name, Value pairs:
--------
Import 10 rows of the first 5 variables from the worksheet named '2007'.
M = readmatrix('airlinesmall_subset.xlsx','Sheet','2007','Range','A2:E11')

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Sept. de 2021
You tried to convert csvread() to readmatrix(). csvread() permits you to pass a parameter that is the number of rows to skip, and then a parameter that is the number of columns to skip. So csvread(filename,1,0) would mean that the first row was to be skipped.
In any modern version of MATLAB, you should be able to use
Data = readmatrix(folder_read(i).name)
readmatrix() should automatically detect and discard any variable names in the first row.
However, I notice that you are using R2019a, which was before some improvements were made to the read*() functions. You might have to use
Data = readmatrix(folder_read(i).name, 'NumHeaderLines', 1);
  2 comentarios
Sándor Burian
Sándor Burian el 26 de Sept. de 2021
Yes, I tried to use readmatrix() instead of csvread() because Help Center suggested it: csvread is not recommended. Use readmatrix instead . Because I tried that too and I got File not found error, which is absolutely incorrect, because if I put folder_read(i).name inside the for, loop then I get back the .csv name.
Anyway, I tried Data = readmatrix(folder_read(i).name, 'NumHeaderLines', 1); as you suggested, and I got this error: Error using readmatrix (line 146) Unable to find or open 'my.csv'. Check the path and filename or file permissions.
Walter Roberson
Walter Roberson el 26 de Sept. de 2021
path = uigetdir();
folder_read = dir(strcat(path,'\my\path\extended\*.csv'));
filenames = fullfile({folder_read.folder}, {folder_read.name});
nfile = numel(filenames);
all_data = cell(nfile, 1);
for i = 1 : nfile
thisfile = filenames{i};
Data = readmatrix(thisfile, 'NumHeaderLines', 1);
all_data{i} = Data;
end;

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by