How to open a directory in Matlab ? Like how do I make the code open a directory ?
11 次查看(过去 30 天)
显示 更早的评论
I need to create a function to plot a matrix. But the matrix is a csv file.
On this purpose, I need to make the code open the directory where the files are. Than I need make the code open the files one by one and create a plot with each one of thoses files until the is no more files to plot.
I'm a beginner on Matlab (started today) so 'm kinda lost
% Root directory of the data files:
Folder = fullfile([Path]), ([folder_name]); % Subfolder inside;
% The directory will depend on the computer
% Select the data file :
[~,~,data] = readtable('[file_name]')
% The name of the file will change for all files
For some reasons I can't explain, the 2nd line isn't working and I don't think th others will
0 个评论
回答(1 个)
Steven Lord
2024-4-9
I've copied and pasted your code commented out into this message so I can run some sections of code I wrote.
This line:
%{
% Root directory of the data files:
Folder = fullfile([Path]), ([folder_name]); % Subfolder inside;
%}
is the same as these two lines:
%{
Folder = fullfile([Path])
([folder_name]); % Subfolder inside;
%}
The first line calls fullfile but doesn't pass the folder name into fullfile. The second assigns the value of the variable folder_name to the variable ans but otherwise doesn't have an effect.
In order to call fullfile with the content of both the Path and folder_name variables, you should eliminate the unnecessary square brackets and parentheses.
% Corrected code that calls fullfile with two inputs
Path = matlabroot
folder_name = 'toolbox'
Folder = fullfile(Path, folder_name)
Looking later in your code:
%{
% The directory will depend on the computer
% Select the data file :
[~,~,data] = readtable('[file_name]')
% The name of the file will change for all files
%}
This doesn't do what you think it does. It does not pass the contents of the variable name file_name into the readtable function. It passes the literal text [file_name] into readtable.
To pass the contents of the variable named file_name into the function, eliminate the single quotes and the square brackets.
%{
% Correct approach to pass file_name into readtable
[~, ~, data] = readtable(file_name)
%}
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!