cd error - file path not character vector Matlab












1















This is probably and easy matlab question but I am really struggling with this one:



I am building a for loop to go through a directory of folders and open a file (filename.csv) within each uniquely named folder. Thus, I have defined my filepath within my loop so that it opens each correct folder and then the correct file within. I am getting hung up, however, on simply concatenating my filepath within the loop, changing the directory to the appropriate folder and then opening the file. Here is the code (outside the for loop with just i=1, for simplicity sake):



drive = dir()
namelist = dir(drive)
filepath = strcat(drive, namelist[1])
cd(filepath)
x = xlsread('filename.csv')


I have also tried defining the filepath as the path of the file itself:



filepath = strcat(drive, namelist[1], 'filename.csv')
x = xlsread(filepath)


Both methods produce an error message when using cd or when using xlsread that 'arguments must contain a character vector'.



I have also tried using fullfile instead of strcat, to no avail.










share|improve this question




















  • 1





    What is dir? Do you have a variable or function defined with that name? dir is a built-in function which returns the file list as a struct array. Your usage does not seem to suggest that you are calling the built-in function.

    – Navan
    Nov 19 '18 at 19:26
















1















This is probably and easy matlab question but I am really struggling with this one:



I am building a for loop to go through a directory of folders and open a file (filename.csv) within each uniquely named folder. Thus, I have defined my filepath within my loop so that it opens each correct folder and then the correct file within. I am getting hung up, however, on simply concatenating my filepath within the loop, changing the directory to the appropriate folder and then opening the file. Here is the code (outside the for loop with just i=1, for simplicity sake):



drive = dir()
namelist = dir(drive)
filepath = strcat(drive, namelist[1])
cd(filepath)
x = xlsread('filename.csv')


I have also tried defining the filepath as the path of the file itself:



filepath = strcat(drive, namelist[1], 'filename.csv')
x = xlsread(filepath)


Both methods produce an error message when using cd or when using xlsread that 'arguments must contain a character vector'.



I have also tried using fullfile instead of strcat, to no avail.










share|improve this question




















  • 1





    What is dir? Do you have a variable or function defined with that name? dir is a built-in function which returns the file list as a struct array. Your usage does not seem to suggest that you are calling the built-in function.

    – Navan
    Nov 19 '18 at 19:26














1












1








1








This is probably and easy matlab question but I am really struggling with this one:



I am building a for loop to go through a directory of folders and open a file (filename.csv) within each uniquely named folder. Thus, I have defined my filepath within my loop so that it opens each correct folder and then the correct file within. I am getting hung up, however, on simply concatenating my filepath within the loop, changing the directory to the appropriate folder and then opening the file. Here is the code (outside the for loop with just i=1, for simplicity sake):



drive = dir()
namelist = dir(drive)
filepath = strcat(drive, namelist[1])
cd(filepath)
x = xlsread('filename.csv')


I have also tried defining the filepath as the path of the file itself:



filepath = strcat(drive, namelist[1], 'filename.csv')
x = xlsread(filepath)


Both methods produce an error message when using cd or when using xlsread that 'arguments must contain a character vector'.



I have also tried using fullfile instead of strcat, to no avail.










share|improve this question
















This is probably and easy matlab question but I am really struggling with this one:



I am building a for loop to go through a directory of folders and open a file (filename.csv) within each uniquely named folder. Thus, I have defined my filepath within my loop so that it opens each correct folder and then the correct file within. I am getting hung up, however, on simply concatenating my filepath within the loop, changing the directory to the appropriate folder and then opening the file. Here is the code (outside the for loop with just i=1, for simplicity sake):



drive = dir()
namelist = dir(drive)
filepath = strcat(drive, namelist[1])
cd(filepath)
x = xlsread('filename.csv')


I have also tried defining the filepath as the path of the file itself:



filepath = strcat(drive, namelist[1], 'filename.csv')
x = xlsread(filepath)


Both methods produce an error message when using cd or when using xlsread that 'arguments must contain a character vector'.



I have also tried using fullfile instead of strcat, to no avail.







matlab vector character cd strcat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 22:33









Banghua Zhao

1,2851719




1,2851719










asked Nov 19 '18 at 19:04









user1554925user1554925

133




133








  • 1





    What is dir? Do you have a variable or function defined with that name? dir is a built-in function which returns the file list as a struct array. Your usage does not seem to suggest that you are calling the built-in function.

    – Navan
    Nov 19 '18 at 19:26














  • 1





    What is dir? Do you have a variable or function defined with that name? dir is a built-in function which returns the file list as a struct array. Your usage does not seem to suggest that you are calling the built-in function.

    – Navan
    Nov 19 '18 at 19:26








1




1





What is dir? Do you have a variable or function defined with that name? dir is a built-in function which returns the file list as a struct array. Your usage does not seem to suggest that you are calling the built-in function.

– Navan
Nov 19 '18 at 19:26





What is dir? Do you have a variable or function defined with that name? dir is a built-in function which returns the file list as a struct array. Your usage does not seem to suggest that you are calling the built-in function.

– Navan
Nov 19 '18 at 19:26












1 Answer
1






active

oldest

votes


















1














dir() return a struct array in your current directory. So drive = dir() will give you a struct array drive. For example:



drive = 
81×1 struct array with fields:
name
folder
date
bytes
isdir
datenum


According to your problem, first, to get a list of directory names, you can do this:



drive = dir()
namelist = {drive([drive(:).isdir]).name}


This will give you a cell array of directory names.



Since . and .. are the current directory and parent directory. You might want to delete these two (Usually, they are the first and second element):



namelist(1) = 
namelist(1) =


Then, to get to the path of those directories, you can do this:



for i =1:length(namelist)
filepath = strcat(pwd, '', namelist{i},'filename.csv')
x = csvread(filepath)
end





share|improve this answer


























  • The find is unnecessary in namelist = {drive(find([drive(:).isdir])).name}, logical indexing with namelist = {drive([drive(:).isdir]).name} will work.

    – Brice
    Nov 20 '18 at 8:55











  • Right! Thanks for the catch!

    – Banghua Zhao
    Nov 20 '18 at 15:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381075%2fcd-error-file-path-not-character-vector-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














dir() return a struct array in your current directory. So drive = dir() will give you a struct array drive. For example:



drive = 
81×1 struct array with fields:
name
folder
date
bytes
isdir
datenum


According to your problem, first, to get a list of directory names, you can do this:



drive = dir()
namelist = {drive([drive(:).isdir]).name}


This will give you a cell array of directory names.



Since . and .. are the current directory and parent directory. You might want to delete these two (Usually, they are the first and second element):



namelist(1) = 
namelist(1) =


Then, to get to the path of those directories, you can do this:



for i =1:length(namelist)
filepath = strcat(pwd, '', namelist{i},'filename.csv')
x = csvread(filepath)
end





share|improve this answer


























  • The find is unnecessary in namelist = {drive(find([drive(:).isdir])).name}, logical indexing with namelist = {drive([drive(:).isdir]).name} will work.

    – Brice
    Nov 20 '18 at 8:55











  • Right! Thanks for the catch!

    – Banghua Zhao
    Nov 20 '18 at 15:29
















1














dir() return a struct array in your current directory. So drive = dir() will give you a struct array drive. For example:



drive = 
81×1 struct array with fields:
name
folder
date
bytes
isdir
datenum


According to your problem, first, to get a list of directory names, you can do this:



drive = dir()
namelist = {drive([drive(:).isdir]).name}


This will give you a cell array of directory names.



Since . and .. are the current directory and parent directory. You might want to delete these two (Usually, they are the first and second element):



namelist(1) = 
namelist(1) =


Then, to get to the path of those directories, you can do this:



for i =1:length(namelist)
filepath = strcat(pwd, '', namelist{i},'filename.csv')
x = csvread(filepath)
end





share|improve this answer


























  • The find is unnecessary in namelist = {drive(find([drive(:).isdir])).name}, logical indexing with namelist = {drive([drive(:).isdir]).name} will work.

    – Brice
    Nov 20 '18 at 8:55











  • Right! Thanks for the catch!

    – Banghua Zhao
    Nov 20 '18 at 15:29














1












1








1







dir() return a struct array in your current directory. So drive = dir() will give you a struct array drive. For example:



drive = 
81×1 struct array with fields:
name
folder
date
bytes
isdir
datenum


According to your problem, first, to get a list of directory names, you can do this:



drive = dir()
namelist = {drive([drive(:).isdir]).name}


This will give you a cell array of directory names.



Since . and .. are the current directory and parent directory. You might want to delete these two (Usually, they are the first and second element):



namelist(1) = 
namelist(1) =


Then, to get to the path of those directories, you can do this:



for i =1:length(namelist)
filepath = strcat(pwd, '', namelist{i},'filename.csv')
x = csvread(filepath)
end





share|improve this answer















dir() return a struct array in your current directory. So drive = dir() will give you a struct array drive. For example:



drive = 
81×1 struct array with fields:
name
folder
date
bytes
isdir
datenum


According to your problem, first, to get a list of directory names, you can do this:



drive = dir()
namelist = {drive([drive(:).isdir]).name}


This will give you a cell array of directory names.



Since . and .. are the current directory and parent directory. You might want to delete these two (Usually, they are the first and second element):



namelist(1) = 
namelist(1) =


Then, to get to the path of those directories, you can do this:



for i =1:length(namelist)
filepath = strcat(pwd, '', namelist{i},'filename.csv')
x = csvread(filepath)
end






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 15:29

























answered Nov 19 '18 at 20:40









Banghua ZhaoBanghua Zhao

1,2851719




1,2851719













  • The find is unnecessary in namelist = {drive(find([drive(:).isdir])).name}, logical indexing with namelist = {drive([drive(:).isdir]).name} will work.

    – Brice
    Nov 20 '18 at 8:55











  • Right! Thanks for the catch!

    – Banghua Zhao
    Nov 20 '18 at 15:29



















  • The find is unnecessary in namelist = {drive(find([drive(:).isdir])).name}, logical indexing with namelist = {drive([drive(:).isdir]).name} will work.

    – Brice
    Nov 20 '18 at 8:55











  • Right! Thanks for the catch!

    – Banghua Zhao
    Nov 20 '18 at 15:29

















The find is unnecessary in namelist = {drive(find([drive(:).isdir])).name}, logical indexing with namelist = {drive([drive(:).isdir]).name} will work.

– Brice
Nov 20 '18 at 8:55





The find is unnecessary in namelist = {drive(find([drive(:).isdir])).name}, logical indexing with namelist = {drive([drive(:).isdir]).name} will work.

– Brice
Nov 20 '18 at 8:55













Right! Thanks for the catch!

– Banghua Zhao
Nov 20 '18 at 15:29





Right! Thanks for the catch!

– Banghua Zhao
Nov 20 '18 at 15:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381075%2fcd-error-file-path-not-character-vector-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()