Is it possible to use vertcat with dot notation?
古いコメントを表示
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 件のコメント
Jiri Hajek
2022 年 12 月 13 日
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
2022 年 12 月 13 日
Jonas
2022 年 12 月 13 日
i gues you wanted
{filename.name}
Sim
2022 年 12 月 13 日
Jiri Hajek
2022 年 12 月 13 日
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
2022 年 12 月 13 日
採用された回答
その他の回答 (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 件のコメント
"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
2022 年 12 月 14 日
Jonas
2022 年 12 月 14 日
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}
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!