Read CSVs from a distant folder with namelist

Hello, I have the names of my files saved in names like:
d = dir('/home/christian/Documents/Data/PressureN_t*.inp');
names = {d.name};
Now I want to csvread all of these files now, but I didn't managed to do it. The problem is that my csvread looks like:
M = csvread('home/christian/Documents/Data/[...]');
where [...] is unknown as I don't know how to do it.
Any suggestions?

4 Comments

Stephen23
Stephen23 on 26 Oct 2017
Edited: Stephen23 on 26 Oct 2017
@Christian Berwanger: this is covered quite well in the MATLAB documentation:
and is well explained on this forum, e.g.:
Note that you will need to use fullfile to generate the full filepath as the above faq shows. If you search this forum you will find many answers that explain how to do this, e.g.:
Yes, I did. But I never found it that way. The difference is that in other question they do not need to include the path to the folder. I need to do so and I cannot combine the path with the variable name. That is where my problem is and I didn't saw it on other posts.
Stephen23
Stephen23 on 26 Oct 2017
Edited: Stephen23 on 26 Oct 2017
"The difference is that in other question they do not need to include the path to the folder."
Errr... actually both of the examples I gave specify the path to the folder.
"...I cannot combine the path with the variable name"
Why not? What is the problem with using sprintf or concatenating the name onto the directory path? I do this all the time when reading files in a loop. Almost every MATLAB user has done this at some point. What is stopping you from doing this?
Both the faq and the answer that I linked to use explicit folder paths: in their examples the relevant variables are called folder, myFolder, and myPath. I have no idea what you are expecting to see, but follow either of those examples and specify the relevant variable to be the required folder path.
So I did it like in the 3rd example
for i = 1:length(names
fullFileName = fullFile('/home/christian/Documents/Data/',names(i));
M = csvread(fullFileName, 0, 1);
end
And I get the following error: Error using csvread File name must be a character vector.
With fid = fopen(fullFileName, 'rt') has a similar error. And how can I name the files different in my workspace?

Answers (0)

This question is closed.

Products

Asked:

on 26 Oct 2017

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!