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.
0 件のコメント
採用された回答
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 件)
参考
カテゴリ
Help Center および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!