Can somebody help me with this script?

11 views (last 30 days)
Felipe Camilo da Silva
Felipe Camilo da Silva on 14 Feb 2018
Edited: Adam on 14 Feb 2018
I am a new user of Matlab and I am not undertanding this section
clear;
warning off;
allData = [];
myfiles = uigetfile('*.mat','multiselect','on');
if ~iscell(myfiles)
load(myfiles);
allData = data;
else
for n=1:length(myfiles)
load(myfiles{n});
allData = [allData data];
end
end
  2 Comments
Adam
Adam on 14 Feb 2018
Which bit in particular? If you are a new user then the Help is your best friend
doc clear
doc warning
doc uigetfile
doc iscell
doc load
doc for
doc if
etc. They will all explain the various commands in that script, so which bit is troubling you specifically, having looked at the help?
Felipe Camilo da Silva
Felipe Camilo da Silva on 14 Feb 2018
I did not understand the "iscell" command and its application related to the condition created by "if".
At some point it reads that "allData" is an empty array and then it reads that it is equal to "data"
load (myfiles {n}) is also confusing for me.

Sign in to comment.

Answers (1)

Adam
Adam on 14 Feb 2018
Edited: Adam on 14 Feb 2018
iscell
checks if you have a cell array. uigetfile will return a cell array if multiple files are selected, otherwise it will just return a char array.
So ~iscell covers the case of a char array, simply loading it as it is only one file.
The else deals with the case where you have multiple files in a cell array and thus creates a loop which takes each file from the cell array in turn and loads it.
load(myFiles{n})
does this by taking the nth element of the cell array of filenames and loading that since myFiles{n} evaluates to a single filename.
allData = [allData data];
concatenates the most recently loaded file (I assume, it isn't clear what is actually in these files, but from the code I assume each contains just a 'data' variable) input with the previous ones. What kind of format that takes I don't know as the concatenation operator will behave differently if data is a cell array of if it is a numeric array, etc.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!