How do I import Data from a different folder?

So I can't change the directory of the data files so need a way of changing where matlab looks for the data.
The code I have so far is:
list = dir ('data_folder/*.dat'); |%where the .dat files are|
[~,index] = sortrows({list.date}.'); list = list(index); clear index |%Sorts them out by date|
for i = 1:length(list);
A(:,i) = importdata(list(i).name);
end
Thank you so much for your help!

Antworten (3)

Image Analyst
Image Analyst am 13 Dez. 2014

0 Stimmen

Try this:
% Get path to a sub folder off the current working directory.
filePattern = full file(pwd, 'data_folder/*.dat');
list = dir (filePattern );
You can replace pwd by any hard coded string if you want, or else call uigetdir() to let the user browse.

4 Kommentare

Using the code:
filePattern = fullfile(pwd, 'data_folder/*.dat');
list = dir (filePattern );
[~,index] = sortrows({list.date}.'); list = list(index); clear index
for i = 1:length(list);
A(:,i) = importdata(list(i).name);
end
I get the error:
Error using importdata (line 136)
Unable to open file.
Error in load_data_test (line 11)
A(:,i) = importdata(list(i).name);
it still does not recognise the directory...
list does not have the folder in it. You have to create the full filename with fullfile
fullFileName = fullfile(pwd, list(i).name);
A(:,i) = importdata(fullFileName);
fullFileName is C:\xx\xx\composite1.dat where it should be C:\xx\xx\data_folder\composite1.dat
How do I get it to add the data_folder part?
My code is:
filePattern = fullfile(pwd, 'data_folder/*.dat');
list = dir (filePattern );
[~,index] = sortrows({list.date}.'); list = list(index); clear index
for i = 1:length(list);
fullFileName = fullfile(pwd, list(i).name);
A(:,i) = importdata(fullFileName);
end
Currently get the error:
Error using importdata (line 136) Unable to open file.
Error in load_data_test (line 15) A(:,i) = importdata(fullFileName);
:(
Dropbox406
Dropbox406 am 17 Dez. 2014
The other way to do it is to add 'data_folder/' in front of all the names of the file directories in the list, would that be easier?

Melden Sie sich an, um zu kommentieren.

Thorsten
Thorsten am 17 Dez. 2014

0 Stimmen

Easy as
addpath('C:\xx\xx\data_folder\composite1.dat')

2 Kommentare

Dropbox406
Dropbox406 am 17 Dez. 2014
Problem is that I need to send it to a different computer so can't add the very specific 'C:\xx\xx\' part at the beginning as it will not run on other computers..
Thorsten
Thorsten am 17 Dez. 2014
Bearbeitet: Thorsten am 17 Dez. 2014
If it's just one other computer, just add both paths to your Matlab path. One path would be invalid on either computer, but this gives just a warning that you can savely ignore.

Melden Sie sich an, um zu kommentieren.

Suhas gowda
Suhas gowda am 6 Mai 2023

0 Stimmen

This works well for me
[file,path] = uigetfile('*.mat'); % opens file selection dialog box. You may choose data from different folder
A=importdata(strcat(path,file)); % data will be imported from the specified path

Kategorien

Gefragt:

am 13 Dez. 2014

Beantwortet:

am 6 Mai 2023

Community Treasure Hunt

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

Start Hunting!

Translated by