필터 지우기
필터 지우기

Problem concatenating two strings

조회 수: 1 (최근 30일)
Allen Hammack
Allen Hammack 2022년 7월 27일
댓글: Allen Hammack 2022년 7월 28일
I have three files (attached). I need the for loop to extract the part of each file name between the hypens into separate fields of the structure data_vel. Here is my script:
data_vel{1}.files = dir(fullfile(pwd,'*.txt'));
j=1;
for j = 1:length(data_vel{1}.files)
data_vel{1}.file_name{j,1} = data_vel{1}.files(j).name;
data_vel{1}.id{j,1} = extractBetween(data_vel{1}.file_name{j},'-','-');
data_vel{1}.field_name(j) = strcat('loc_',data_vel{1}.id(j));
data_vel{1}.(data_vel{1}.field_name(j)).velocity_exist = 'yes';
end
I am getting an error, which I think is caused by this line
data_vel{1}.field_name(j) = strcat('loc_',data_vel{1}.id(j));
I want this line to add "loc_" to the beginning of each data_vel{1} value. So, I want "loc_1A", "loc_3B", and "loc_9C" to be created, which I would then assign as field names in the data_vel{1} structure. Currently, This line produces a 1x2 cell instead of a 1x1 cell. I haven't been able to figure out how to create a 1x1 structure.
Can someone please help?

채택된 답변

dpb
dpb 2022년 7월 27일
편집: dpb 2022년 7월 27일
I hate struct, but if I understand the Q?, then try something like --
d=dir(fullfile(pwd,'test-*.txt'));
fn=strcat('loc_',extractBetween({d.name},'-','-'));
fnv=[fn; cell(1,numel(fn))];
deal_vec=struct(fnv{:});
which results in
>> deal_vec =
struct with fields:
loc_1A: []
loc_3B: []
loc_9C: []
>>
  댓글 수: 1
Allen Hammack
Allen Hammack 2022년 7월 28일
You solved the problem! Thank you so much!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by