Is it possible to use vertcat with dot notation?

1 次查看(过去 30 天)
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
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
Sim 2022-12-13
I am extremely thankful to all of you @Jiri Hajek @DGM @Jonas @Stephen23 for your comments (cool explanation @Stephen23!).... and at the same time, really sorry for my very silly question.... I did not think at all about {filename.name}... sorry! (In any case, if any of you wants to put your comments in the Answer section I will accept it with pleasure :-) )

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2022-12-13
移动:Stephen23 2022-12-13
"The error is being caused by the fact that filename is not a struct."
FILENAME is a struct (as returned by DIR). Curly brace indexing is not defined for structures (see the error message).
"Is it possible to use vertcat with dot notation?"
Of course, you can use any comma-separated list as the inputs to VERTCAT:
S = struct('X',{'hello','world'}) % content have same number of columns...
S = 1×2 struct array with fields:
X
vertcat(S.X) % so VERTCAT is possible but fragile. Creates a character matrix.
ans = 2×5 char array
'hello' 'world'
But I would not recommend it because it is very fragile code: using VERTCAT on the structure from DIR() assumes that all of the filenames have exactly the same length, otherwise your code will throw an error:
S = struct('X',{'a','bcd'}) % content have different number of columns...
S = 1×2 struct array with fields:
X
vertcat(S.X) % which is why VERTCAT is a bad approach.
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
As Jonas correctly wrote, you should probably use a cell array: {filename.name}
Or, simply refer directly to the content of the structure array.
  1 个评论
Sim
Sim 2022-12-14
Thanks to all of you for your precious contribution @Stephen23, @Jonas, @Peter Perkins, @Jiri Hajek, @DGM.
Personally, I would have accepted all your answers/comments, but there is only the possibility to accept one. :-)
@MathWorks Support Team I think it might be nice (just an idea/suggestion) to have the possibility to accept multiple answers in some cases. :-)

请先登录,再进行评论。

更多回答(2 个)

Peter Perkins
Peter Perkins 2022-12-13
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
s = 2×1 struct array with fields:
name folder date bytes isdir datenum
t = struct2table(s)
t = 2×6 table
name folder date bytes isdir datenum ______ ____________________________ ________________________ _____ _____ __________ {'.' } {'/users/mss.system.cHQNIJ'} {'13-Dec-2022 16:06:16'} 0 true 7.3887e+05 {'..'} {'/users/mss.system.cHQNIJ'} {'13-Dec-2022 16:06:16'} 0 true 7.3887e+05
  2 个评论
Stephen23
Stephen23 2022-12-13
编辑:Stephen23 2022-12-13
"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 = 2×6 table
name folder date bytes isdir datenum ______ ____________________________ ________________________ _____ _____ __________ {'.' } {'/users/mss.system.rBKUO2'} {'13-Dec-2022 16:16:37'} 0 true 7.3887e+05 {'..'} {'/users/mss.system.rBKUO2'} {'13-Dec-2022 16:16:37'} 0 true 7.3887e+05
T.name % cell array, not vertical concatenation of char vectors.
ans = 2×1 cell array
{'.' } {'..'}
Sim
Sim 2022-12-14
Thanks to all of you for your precious contribution @Stephen23, @Jonas, @Peter Perkins, @Jiri Hajek, @DGM.
Personally, I would have accepted all your answers/comments, but there is only the possibility to accept one. :-)
@MathWorks Support Team I think it might be nice (just an idea/suggestion) to have the possibility to accept multiple answers in some cases. :-)

请先登录,再进行评论。


Jonas
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}
  1 个评论
Sim
Sim 2022-12-14
Thanks to all of you for your precious contribution @Stephen23, @Jonas, @Peter Perkins, @Jiri Hajek, @DGM.
Personally, I would have accepted all your answers/comments, but there is only the possibility to accept one. :-)
@MathWorks Support Team I think it might be nice (just an idea/suggestion) to have the possibility to accept multiple answers in some cases. :-)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by