Error using readtable, Input must be a row vector of characters or string scalar: WHERE IS YOUR PROBLEM??

Hi guys.
My problem is rather common but I cannot solve it. I have several .csv files that I want to combine into one (same sheet). I am trying
vars={'Temperature'; 'Relative humidity'};
path = append(output_path,'\',stations{i},'\');
files = append(path, 'Stats ', months ,' ', stations{i} ,' ',vars{1},' .csv');
result = table();
for fidx = 1:numel(files)
filecontent = readtable(files);
[~, filenumber] = fileparts(files(fidx).name);
filecontent.file_label = repmat(str2double(filenumber), height(filecontent), 1);
result = [result; filecontent];
end
Just to help you understand, stations{i} come from the attached .xlsx and the attached .csv is one of the many files I want to combine. When I run these lines I get the message "Error using readtable, Input must be a row vector of characters or string scalar". I tried files = char(files) but it didn't work.
Any ideas please???
PS. I am on 2019a.

 Risposta accettata

readtable() can read one file at a time. Your "files" variable seems to contain multiple files. So you can't use readtable(files). Most likely, you need to use readtable(files{fidx})
Whenever there is an error, try to debug it yourself. Put a break point in the code, run the code line by line, check out the value of each variable, and look at the error message.

7 Commenti

Great, I tried what you suggested and it worked. Of course I tried to debug myself but obviously I missed it. Thank you for your help!
Nevertheless, now it stops in the next line with the error "Dot indexing is not supported for variables of this type."
Any ideas?
Your files variable is not the output of a dir() call. It appears to be a cell array of character vectors.
Exactly Walter. I used append in the 3rd line instead of dir because the filenames I want to "extract" are dynamic, AKA months ,stations{i} and vars change.
What can I do to overcome this?
which line causes the new error message? Your code requires output_path,stations{i}, etc. which are not provided.
This line causes the problem: [~, filenumber] = fileparts(files(fidx).name);
stations{i} come from the attached .xlsx and based on the weather station names I created respective folders.
output_path is the path where all .csv are saved in my computer.
I am also attaching 2 .csv to help you understand.
so pause your running program at this line, check the value of "fidx", "files" and run "files(fidx)", "files(fidx).name" in Command Window. You will see the exact same error message. You have to ask youself what do you mean and why do you run "files(fidx).name"?
You were right. I fixed my code as follows, in case someone has the same problem with me:
path = append(output_path,'\',stations{i},'\');
files = append(path, 'Stats ', months ,' ', stations{i} ,' ',vars{1},' .csv');
result = table();
for fidx = 1:numel(files)
filecontent = readtable(files{fidx});
[~, filenumber] = fileparts(files{fidx});
filecontent.file_label = repmat(str2double(filenumber), height(filecontent), 1);
result = [result; filecontent];
end
outfile = [output_path,'\',stations{i},'\','Monthly T stats ',stations{i},' 2015.xlsx'];
writetable(result,outfile);
Thanks everyone for your contribution!

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by