MATLAB Answers

Loading files from the folder

3 ビュー (過去 30 日間)
hamzah khan
hamzah khan 2020 年 1 月 24 日
コメント済み: Geoff Hayes 2020 年 1 月 27 日
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 ?

  0 件のコメント

サインイン to comment.

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 1 月 24 日
編集済み: Geoff Hayes 2020 年 1 月 24 日
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 件のコメント

表示 4 件の古いコメント
Geoff Hayes
Geoff Hayes 2020 年 1 月 24 日
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
hamzah khan
hamzah khan 2020 年 1 月 25 日
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
Geoff Hayes
Geoff Hayes 2020 年 1 月 27 日
I don't undertstand the purpose of including the uigetfile in the signature
function [out] = DataLoading(uigetfile)
This input parameter is unnecessary.

サインイン to comment.

その他の回答 (0 件)

サインイン してこの質問に回答します。


Translated by