Cell Array always includes a period as its first value
Mostra commenti meno recenti
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 Commento
Stephen23
il 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?
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su File Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!