Borrar filtros
Borrar filtros

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

12 visualizaciones (últimos 30 días)
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

Respuestas (1)

Steven Lord
Steven Lord el 9 de Abr. de 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)
%}

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by