How do I extract a vector from a single field in a structured variable?

조회 수: 2 (최근 30일)
I am trying to create a vector variable from a field of an 8x1 structure.
To create the structure I used the dir function - I am trying to create a vector of subfolders name and set the code up so that it can take out the names if different folders were in the original folder.
I type:
folders_temp = dir('data_CLEANED');
folder_names = folders_temp(~ismember({folders_temp.name},{'.','..'})); %just doing this to get rid of dot and dot dot
participant_ID = [folder_names.name]
The participant names all crammed together in a single cell
Is there a way to create a vector with each participant code having its own cell?
  댓글 수: 4
Voss
Voss 2022년 1월 18일
Use {} rather than [], like in my answer.
Stephen23
Stephen23 2022년 1월 18일
편집: Stephen23 2022년 1월 18일
"... when I want it to return [MH, TM, JW]"
In MATLAB square brackets are a concatenation operator, not a list operator like in some other languages.
What you need is a cell array, just as Benjamin shows: https://www.mathworks.com/help/matlab/cell-arrays.html

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

채택된 답변

Voss
Voss 2022년 1월 18일
Maybe this?
folder_names = struct('name',{'a','bb','ccc'})
folder_names = 1×3 struct array with fields:
name
participant_ID = {folder_names.name}
participant_ID = 1×3 cell array
{'a'} {'bb'} {'ccc'}

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by