필터 지우기
필터 지우기

Using sprintf to get variables from a file

조회 수: 1 (최근 30일)
Isma_gp
Isma_gp 2016년 10월 11일
댓글: Guillaume 2016년 10월 12일
Hi,
I would like to extract some data from a file. The data comes with different variable names. For example, in order to get the names of the fields of interest called "FORCE_SH10", "FORCE_SH20", I use the following:
names_f = arrayfun(@(n)sprintf('FORCE_SH%02d',n),10:10:40,'Uni',false);
I would like a similar way to retrieve the following names: "FORCE_x_y" where x can be 1:4 and y can be 1:4 (with all possible combinations i.e. "FORCE_1_2", "FORCE_3_4" etc.)
Thanks

채택된 답변

Guillaume
Guillaume 2016년 10월 11일
[x, y] = ndgrid(1:4, 1:4); %first 1:4 is x range, 2nd 1:4 is y range
names_f = arrayfun(@(xx, yy) sprintf('FORCE_%d_%d', xx, yy), x(:), y(:), 'UniformOutput', false)
Or if you're using the new string class introduced in R2016b:
[x, y] = ndgrid(1:4, 1:4); %first 1:4 is x range, 2nd 1:4 is y range
names_f = compose('FORCE_%d_%d', x(:), y(:))
  댓글 수: 2
Isma_gp
Isma_gp 2016년 10월 12일
I get an error: Undefined function 'compose' for input arguments of type 'char'.
Guillaume
Guillaume 2016년 10월 12일
편집: Guillaume 2016년 10월 12일
As I wrote:
"Or if you're using the new string class introduced in R2016b"
You're obviously not using R2016b, so use the first solution.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Author Block Masks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by