How to extract the first column of a structure array and save it as a different array

조회 수: 198 (최근 30일)
I have a structure array containing the names of my files in the first column.
I want to extract only the names of my files and store it in a different array.
I tried the following but it does not work:
% My struct array with 15560 elements where the name in the first column
files = dir('seaice_conc_daily_nh_*_v04r00*.nc');
%I tried this would work but it only returns the name of the first
%element...
filenames = files.name
%I know I can call individual elements i with:
filenames_i = files(i).name
%But I want a new array with all the name of my files
Thank you
  댓글 수: 2
Stephen23
Stephen23 2022년 7월 15일
"I have a structure array containing the names of my files in the first column."
Actually the names are stored in the field 'name'. The size of a structure is independent of how many fields it has:

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2022년 7월 15일
편집: Stephen23 2022년 7월 15일

추가 답변 (2개)

Ruchika P Barman
Ruchika P Barman 2022년 7월 13일
It is my understanding that you have a structure array containing the names of your files in the first column of the structure array and are trying to extract only the names of the files and store it in a different array. The following code should help you achieve the same.
filenames = vertcat(files.name)
  댓글 수: 1
A LL
A LL 2022년 7월 14일
Thank you for your answer.
This is not super convenient since it creates an array of size (length(files)) X (# of character in the name of the file). It stores each character of the name of the file in an individual character cell. I wanted to have an array of size (length(files)) X 1 with the name of the files in a single cell.

댓글을 달려면 로그인하십시오.


Tasneem
Tasneem 2022년 7월 13일
It is my understading that you are trying to extract first column from an array. so you could use files(:,1) to extract the first column and store the output in a different array.
The below is the code.
files = dir('seaice_conc_daily_nh_*_v04r00*.nc');
filenames = files(:,1);
  댓글 수: 1
A LL
A LL 2022년 7월 14일
Thank you for your answer. However, this command only creates the same structure array I had initially i.e. the first column is the name, the second is the folder, third is the date, fourth is the bytes and etc.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by