Append a char vector to a subset of cells (whose indices are known) in cell array of character vectors

조회 수: 2 (최근 30일)
Hello MATLAB community:
I am trying to determine the most effective way of going about the following task. I have a cell array of character vectors: mycell = {'cal_start', 't_001', 't_002', ..., 't_200', 'cal_end', 'exp_start', 't_001', 't_02' ,..., 't_048', 'exp_end'};
I have defined the relevant indices from this cell array, the contents of which I would like to append a character vector. In fact there are two index ranges I have defined, and these index ranges indentify the cells between 'cal_start" and 'cal_end', and 'exp_start' and 'exp_end'.
For example:
% regarding the below, for context, 't' is trial, 'cal' is calibration,
% and 'exp' is experiment.
mycell = {'cal_start', 't_001', 't_002', 't_003', 't004', 'cal_end', 'exp_start',...
't_001', 't_002', 'exp_end'};
a = find(contains(mycell, 'cal'));
% i.e., a = [1,6]
b = find(contains(mycell, 'exp'));
% i.e., b = [7,10]
% define trial indices comprising the calibration phase
calidx_rng = a(1)+1 : a(2)-1;
% define trial indices comprising the experiment phase
expidx_rng = b(1)+1 : b(2)-1;
Now, for the charcater vectors of mycell contained in calidx_rng, I want to append 'cal_' at the beginning of each, such that you now have:
mycell = {'cal_start', 'cal_t_001', 'cal_t_002', cal_t_003', cal_t_004', 'cal_end', 'exp_start', 't_001', 't_002', 'exp_end'}
And, similarly, I want to perform the "same" operation, but appending 'exp_' to mycell(expidx_rng) so that the final product is:
mycell = {'cal_start', 'cal_t_001', 'cal_t_002', cal_t_003', cal_t_004', 'cal_end', 'exp_start', 'exp_t_001', 'exp_t_002', 'exp_end'};
Sorry, there was a premature submission when I was adding tags, to finish my post, this is what I have attempted (though here I am continuing in the context of the simplified example above):
mynewcell = append('cal_', mycell(calidx_rng));
MATLAB gives ther error:
Wrong number of input arguments for obsolete matrix-based syntax.
My suspicion is that the best way to approach this is using the cellfun or arrayfun functions, but I must admit, the examples provided in the documentation don't give me a clear picture as to how I should use them in my case.
This is probably a stupid question, but thanks for your time.

채택된 답변

Stephen23
Stephen23 2019년 6월 30일
편집: Stephen23 2019년 6월 30일
Your basic concept is correct, you just need to use indexing on the LHS as well as the RHS:
mycell(calidx_rng) = strcat('cal_', mycell(calidx_rng));
mycell(expidx_rng) = strcat('exp_', mycell(expidx_rng));
% ^^^^^^^^^^^^ you need this!
Giving:
>> mycell{:}
ans = cal_start
ans = cal_t_001
ans = cal_t_002
ans = cal_t_003
ans = cal_t004
ans = cal_end
ans = exp_start
ans = exp_t_001
ans = exp_t_002
ans = exp_end

추가 답변 (1개)

Raymond MacNeil
Raymond MacNeil 2019년 6월 30일
Alright, this was easily achieved with the following (continuing from the example given abvove):
mynewcell = strcat('cal_'), mycell(calidx_rng));
mynewcell = strcat('exp_'), mycell(expidx_rng));
Hope this of help, Ray!
  댓글 수: 1
Raymond MacNeil
Raymond MacNeil 2019년 6월 30일
No, this actually isn't working, as the output gives a truncated array equivalent in length to the index range defined by calidx_rng. You may have concatenate two cell arrays after applying strcat "separately" to 'mycell'. Will explore further and get back to you.

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by