Hello,
I have this code which allows the user to select any folder on the computer and then loads all the .mat files to the directory.
%% Data Acuisition from the folder%%%
pathname = uigetdir;
allfiles = dir(fullfile(pathname,'*.mat'));
out = load(allfiles(1).name);
% load all files in a struct so they can accessed later on
for k = 2: numel(allfiles)
out(k) = load(allfiles(k).name);
end
I want to make changes to it such that, user is able to select some files from the folder and not all of them to be loaded to matlab directory..
Does any one know how can we do it ?

 채택된 답변

Geoff Hayes
Geoff Hayes 2020년 1월 24일
편집: Geoff Hayes 2020년 1월 24일

0 개 추천

hamzah - could you use uigetfile and allow the user to select the files that they are interested in?
[file,path] = uigetfile('*.mat',...
'Select One or More Files', ...
'MultiSelect', 'on');

댓글 수: 7

Chris Dan
Chris Dan 2020년 1월 24일
편집: Chris Dan 2020년 1월 24일
how this will work out? can you please write the full code?
how will I access the data of those files?
You could try to see what happens but I think that you could do something like
[file,path] = uigetfile('*.mat',...
'Select One or More Files', ...
'MultiSelect', 'on');
out = {};
for k = 1:length(file)
out{k} = load(fullfile(path, file{k}));
end
Chris Dan
Chris Dan 2020년 1월 24일
편집: Chris Dan 2020년 1월 24일
thanks :)
Instead of cell array, we can also declare a struct..?
Chris Dan
Chris Dan 2020년 1월 24일
Hi, If I am selecting just one file from the folder, I am getting this error
"Brace indexing is not supported for variables of this type."
Do you kno about it ?
hamzah - it sounds like you'll need a special case for if there is just one file selected in which case file is not a cell array but probably just a string (array of characters).
[files,pathToFiles] = uigetfile('*.mat',...
'Select One or More Files', ...
'MultiSelect', 'on');
out = {};
if ~iscell(files)
out{1} = load(fullfile(pathToFiles, files));
else
for k = 1:length(files)
out{k} = load(fullfile(pathToFiles, files{k}));
end
end
Okay, Thanks :)
I am trying to make function which I can call any where to open the folder
Will this work?
out = DataLoading(uigetfile);
function [out] = DataLoading(uigetfile)
[files,pathToFiles] = uigetfile('*.mat',...
'Select One or More Files', ...
'MultiSelect', 'on');
out = {};
if ~iscell(files)
out{1} = load(fullfile(pathToFiles, files));
else
for k = 1:length(files)
out{k} = load(fullfile(pathToFiles, files{k}));
end
end
end
I don't undertstand the purpose of including the uigetfile in the signature
function [out] = DataLoading(uigetfile)
This input parameter is unnecessary.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2020년 1월 24일

댓글:

2020년 1월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by