Cell Array always includes a period as its first value
4 次查看(过去 30 天)
显示 更早的评论
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
- return the files' data from a folder, through the dir function
- strip the names with fileinfo.name
- break up the filenames (which include extensions and such) into Path, Name and Extension arrays with fileparts
- 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 = {fileinfo.name};
fname={};fext={}; fpath={}
for i=1:size(fdata,2)
[filepath,filename,fextension] = fileparts(fdata(i)); % Breaking down data
fpath{i}=filepath;
fname{i}=filename;
fext{i}=fextension;
end
clear filepath filename fextension
Sample={}; Temp={}; Time={}; Elem={};
for i=1:size(fname,2)
N = strsplit(fname{i},'_'); % Breaking down name
Sample{i}=N(1);
Temp(i)= N(2);
Time(i)=N(3);
Elem(i)=N(4);
end
The problem I am encountering is that
fdata = {fileinfo.name};
returns
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 fileinfo.name 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,
strsplit(fname{i},'_')
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 个评论
Stephen23
2021-2-17
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?
采纳的回答
Stephen23
2021-2-17
编辑:Stephen23
2021-2-17
"So, why does fileinfo.name 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 个评论
Steven Lord
2021-2-17
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))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!