Is it possible to use vertcat with dot notation?
Show older comments
EDITED Is it possible to use vertcat with dot notation?
This is an example once I extract names from Excel files contained in a folder:
%input
filename = dir(fullfile(directory,'*file*'));
% output
>> filename
filename =
3×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
>> filename.name
ans =
'file_1.xlsx'
ans =
'file_2.xlsx'
ans =
'file_3.xlsx'
% try to concatenate vertically the three files' names
>> vertcat(filename{:}.name)
Brace indexing is not supported for variables of this type.
7 Comments
Jiri Hajek
on 13 Dec 2022
Hi, it's not clear, what type data is on your filename. Also the error is related to the fact that you are using braces for the filename, not for the vertcat...
@Jiri Hajek is correct. The error is being caused by the fact that filename is not a struct. Depending on how it was created, it may be empty, a char, a cell, or something else in certain degenerate cases. You'll have to check why it's becoming something other than a struct and safeguard against that case.
For example, if you're using dir() to find a file or the contents of a directory, does the query actually return any results? Does it return more than one result?
Sim
on 13 Dec 2022
Jonas
on 13 Dec 2022
i gues you wanted
{filename.name}
Sim
on 13 Dec 2022
Jiri Hajek
on 13 Dec 2022
Ok, you can put the names into a single variable, but considering the number of characters in each may be different, the most natural choice is cell array (column):
fileNameColumn = {filename.name}';
Sim
on 13 Dec 2022
Accepted Answer
More Answers (2)
Depending on what you are doing, you may find it convenient to turn that struct into a table (which wil automatically vertcat the file names for you):
s = dir
t = struct2table(s)
2 Comments
"which wil automatically vertcat the file names for you"
Strictly speaking it creates a cell array of character arrays, rather than vertical concatenation of the field content:
S = dir();
T = struct2table(S)
T.name % cell array, not vertical concatenation of char vectors.
Sim
on 14 Dec 2022
Jonas
on 14 Dec 2022
if you just want to have all file names available, you could use { }
e.g. in a cell array
{filename.name}
if you do not need further information from the dir out beside the name, you could also abbreviate it to one line
{dir(fullfile(directory,'*file*')).name}
Categories
Find more on Creating and Concatenating Matrices 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!