필터 지우기
필터 지우기

Parametric variable names

조회 수: 6 (최근 30일)
Amarpal
Amarpal 2011년 12월 20일
Hi
Here's my question: Suppose there is an array called 'var' of size 40*1. I want to write a for loop that divides 'var' into four different arrays, say, 'var1','var2','var3' & 'var4' where each of them will pick 10 values from 'var'. So, I want the loop counter, say i, to be used in the array name,i.e.'var' as var{i}.
I'm trying to use the function 'genvarname' to create the parametric names, but I'm not able to assign values to those names...Here's what I'm trying:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% script - newone.m
close
clear all
var = (1:40)';
divsize = 10;
counter = length(var)/divsize;
holdvar = genvarname({'var1','var1','var1','var1'}); % Also I want this step to go parametric....like var1 * ones(counter,1),must generate the number of strings needed. I know the way I've used might be incorrect, but used it just to illustrate the purpose.
%%%%%%%%%%%%%%%%%%%%%%%%
output at this stage:
holdvar =
'var1' 'var11' 'var12' 'var13'
%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:counter
holdvar{i} = var((((i-1)*divsize)+1):(i*divsize));
end
holdvar = cell2mat(holdvar);
%%%%%%%%%%%%
output:
holdvar =
1 11 21 31
2 12 22 32
3 13 23 33
4 14 24 34
5 15 25 35
6 16 26 36
7 17 27 37
8 18 28 38
9 19 29 39
10 20 30 40
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The output I'm looking for is this:
var1 =
1
2
3
4
5
6
7
8
9
10
var11 =
11
12
13
14
15
16
17
18
19
20
var13 =
21
22
23
24
25
26
27
28
29
30
var14 =
31
32
33
34
35
36
37
38
39
40
Could you kindly guide me in the right direction. Also any other approach that does the same job is welcome!
Many Thnaks Amar

답변 (1개)

Sean de Wolski
Sean de Wolski 2011년 12월 20일
The method you have now is the recommended one!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by