Clear Filters
Clear Filters

How to open a directory in Matlab ? Like how do I make the code open a directory ?

11 views (last 30 days)
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

Answers (1)

Steven Lord
Steven Lord on 9 Apr 2024
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
Path = '/MATLAB'
folder_name = 'toolbox'
folder_name = 'toolbox'
Folder = fullfile(Path, folder_name)
Folder = '/MATLAB/toolbox'
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)
%}

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!