MATLAB Answers

Cell Array always includes a period as its first value

18 views (last 30 days)
I'm working with image files which are all labeled something like "152b_800_1000_Ti.psd". [Name_Temp_Time_Element"]
I am going to use the RGB data for graphing, and I would like to automatically add labels to each graph containing the "Name" "Temp" "Time" and "Element" taken from the images' filenames. The code beneath is essentially one part of the whole puzzle, to gather the text for each label from the file names.
This should work to
  1. return the files' data from a folder, through the dir function
  2. strip the names with
  3. break up the filenames (which include extensions and such) into Path, Name and Extension arrays with fileparts
  4. cycle through each Name (in the fname[] array) and break it up further into Sample, Temp,Time and Element using the strsplit function and a '_' delimiter.
% Extracting file info
fileinfo = dir('C:\Users\...\MATLAB Drive\...\Images');
fdata = {};
fname={};fext={}; fpath={}
for i=1:size(fdata,2)
[filepath,filename,fextension] = fileparts(fdata(i)); % Breaking down data
clear filepath filename fextension
Sample={}; Temp={}; Time={}; Elem={};
for i=1:size(fname,2)
N = strsplit(fname{i},'_'); % Breaking down name
Temp(i)= N(2);
The problem I am encountering is that
fdata = {};
and I am not sure how to remove those first 3 columns. I have noticed that the MATLABDRIVE file is craeted everytime I start Matlab.
I have tried to simply delete those cells but it seems that the first one cannot be removed no matter what.
So, why does return those first 3 columns, and how can I get rid of them?
Also, when running the entire thing, I get this error below.
Index exceeds the number of array elements (1).
Error in test (line 29)
Temp(i)= N(2);
Even though,
returns properly.
ans =
1×4 cell array
{'153c'} {'800'} {'1000'} {'Ti'}
Thus, it appears that it is not properly assigend to the N variable.
I'm pretty sure my shoddy understanding of cells v arrays might be the issue here.
  1 Comment
Stephen Cobeldick
Stephen Cobeldick on 17 Feb 2021
Note that you can easily avoid those intermediate variables by allocating directly to the cell arrays:
[fpath{i},fname{i},fext{i}] = fileparts(fdata(i));
Note that fpath will contain only empty character vectors, so is rather superfluous.
If it is not required to have those cell arrays with all names, why not simplify your code by getting rid of the first loop entirely and calling fileparts inside the second loop?

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 17 Feb 2021
Edited: Stephen Cobeldick on 17 Feb 2021
"So, why does return those first 3 columns, and how can I get rid of them?"
Because DIR returns exactly what the OS returns when you ask it for the content of a folder: this includes shortcuts to the folder itself (one dot) and its parent folder (two dots) and apparently your folder also contains something else as well. This is exactly the same as when you go to Windows CMD.exe and type dir, or other similar ways that you can query the OS about folder contents.
This is trivial to avoid by specifying the filename using a wildcard and the required file extension:
D = 'C:\Users\...\MATLAB Drive\...\Images';
fileinfo = dir(fullfile(D,'*.psd'));
% ^ filename wildcard
% ^^^^ file extension
It is also worth knowing that the one dot and two dot names are NOT guaranteed to be the the first two results.
  1 Comment
Steven Lord
Steven Lord on 17 Feb 2021
Another approach would be to only operate on the names of files in the directory, not folders.
loc = fullfile(matlabroot, 'toolbox', 'matlab', 'datafun');
D = dir(loc);
justTheFiles = D(~[D.isdir]);
arrayfun(@disp, justTheFiles(1:5))
name: 'Contents.m' folder: '/MATLAB/toolbox/matlab/datafun' date: '01-Oct-2019 19:49:36' bytes: 4298 isdir: 0 datenum: 7.3770e+05 name: 'bounds.m' folder: '/MATLAB/toolbox/matlab/datafun' date: '22-Oct-2019 16:40:11' bytes: 2007 isdir: 0 datenum: 7.3772e+05 name: 'conv.m' folder: '/MATLAB/toolbox/matlab/datafun' date: '03-Apr-2019 19:43:51' bytes: 1721 isdir: 0 datenum: 7.3752e+05 name: 'conv2.m' folder: '/MATLAB/toolbox/matlab/datafun' date: '03-Oct-2014 22:48:01' bytes: 1228 isdir: 0 datenum: 7.3588e+05 name: 'convn.m' folder: '/MATLAB/toolbox/matlab/datafun' date: '23-Oct-2013 10:15:28' bytes: 929 isdir: 0 datenum: 7.3553e+05

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!